252 questions
4
votes
1
answer
273
views
Implicit type casting within parenthesis and literals in C
The follow derives from the devkitpro 3ds filter audio example though it's not relevant to know exactly how the example works.
In short, consider this code:
size_t data_size = ...;
uint32_t data* = (...
4
votes
2
answers
129
views
What is the purpose of the instructions that gcc emits?
I have an example of C code with an issue (integer promotions from uint16_t to int, and then cast to uint32_t):
#include <stdio.h>
#include <stdint.h>
int main() {
uint16_t x = 48000;
...
4
votes
2
answers
294
views
GCC/Clang-18 compiler assumes type as unsigned int for enum
Hello and thanks for reading,
I just came upon a bug in my C program and while I was able to find out the cause of the bug I am having trouble rationalizing the behaviour of the compiler and I could ...
2
votes
2
answers
101
views
How to prevent type promotion when using two or more templates when next one implements the previous
I'm writing a C++ logging implementation for an embedded system.The idea is to use templates to declare the message id (type) and enforce the arguments. There are constraints that the code I'm writing ...
0
votes
2
answers
114
views
Integer Promotion in C
I read the c documentation but I couldn't understand it. Could you please answer my questions(Maybe my English is not good.)
I have two codes and two question.
Question about first code: Has an ...
2
votes
0
answers
60
views
Flatbuffers VectorIterator::operator- undefined behavior
I was digging into code of flatbuffers and it does seem to me that there is undefined behavior in the core library. But I just can't believe it is there (and it seems to work in some usecases)...
I'd ...
0
votes
2
answers
182
views
Why does reading a file with while ((c = getc(file)) != EOF) work on some platforms?
It is discussed in another thread that reading a file with the following code can result in an infinite loop because EOF is an integer outside the range of char and the while condition therefore ...
6
votes
3
answers
151
views
integer promotion isn't taken into account
Let's take the following code which we compile for a 32 bit system:
typedef unsigned int uint32;
typedef unsigned char uint8;
...
uint8 y = 0xFF;
uint32 x = (y << 24U);
y can be promoted to int ...
35
votes
3
answers
2k
views
Unexpected behaviour during implicit conversion in C
I was helping a student with his assignment and ran into a very gnarly "bug" which I could not explain, and was wondering if I could get help in understanding what exactly is going on.
The ...
-1
votes
2
answers
200
views
Why does "left shift count >= width of type" seemingly kick in late?
For context, I on a 64-bit (AMD64) little-endian system and am using g++ -std=gnu++23 -O3 -Wall to compile my code, if it makes any difference.
The following code snippet all shows some seemingly ...
-1
votes
1
answer
172
views
Numeric promotions in C# [closed]
In C#, why can't we add int to long? Why does the compiler need implicit conversion from int to long to perform the operations?
Similar for byte to short or short to int: why is an implicit conversion ...
0
votes
1
answer
68
views
How to force type promotion in a variadic function wrapper?
I am writing a wrapper to call API functions in a vtable. This is done through a variadic temple, to wrap API functions with an arbitrary number of parameters.
I have found that some calls work only ...
41
votes
4
answers
4k
views
Why is 'char -> int' promotion, but 'char -> short' is conversion (but not promotion)?
I've read cppreference.com's implicit conversion:
Integral promotion:
prvalues of small integral types (such as char) may be converted to prvalues of larger integral types (such as int).
[...]
Note ...
10
votes
1
answer
485
views
Why do plus and minus have different promotion rules although the results are the same?
I wonder why a - b and a + (-b) give the same result but in different types in numpy:
import numpy as np
minuend = np.array(1, dtype=np.int64)
subtrahend = 1 << 63
result_minus = minuend - ...
0
votes
1
answer
67
views
why is the output of this question is 266 -1 10 in turbo c compiler?
int main(){
int a, b;
char *cp;
a = 511;
cp = &a;
b = *cp;
*cp = 10;
printf("%d %d %d", a,b,*cp);
return 0;
}
The output for the above code is 266 -1 10
...
0
votes
2
answers
125
views
Is it expected for sizeof(char) to return 1 instead of 4 when integer promotion takes place in C?
#include <stdio.h>
int main()
{
unsigned char c = 'a';
c = c + 3;
printf("%c ", c);
printf("%zu",sizeof(c));
return 0;
}
Output:d 1
when integer ...
3
votes
0
answers
40
views
Incosistent integer promotion when performing bitwise OR and leftshifting
I'm getting -Wconversion warning in C.
conversion from 'int' to 'uint8_t' {aka 'unsigned char'} may change value [-Wconversion]
When performing the following operation:
uint8_t output_A = 0x00;
...
6
votes
0
answers
154
views
std::byte overhead from integer promotion
Consider an unsigned char v that goes through a series of bit-wise operations with the result stored back to v. Under the hood, it is integer promoted once, undergoes a series of operations, and the ...
6
votes
2
answers
182
views
Why does declaring the same function both with and without parameters not cause compilation errors?
For the code:
int hi(int);
int hi();
int main()
{
hi(3);
}
I don't get any compilation errors (calling hi(); without arguments does get a compilation error).
I expected that the compiler would ...
2
votes
3
answers
112
views
a |= (1 << 31) results in unexepected value
I made a reproducible code as below/
#include <stdio.h>
int main(void)
{
long int a = 0x0;
a |= (1 << 31);
printf("a: 0x%lx\n", a);
}
I expect 'a' should be ...
0
votes
2
answers
1k
views
Misra Rule violation "composite expression shall not be assigned to an object with wider essential type" and Integral promotion
I have below function to check some count value and update the final count.
uint16 final_count = 0U;
uint8 count1 = 0U;
uint8 count2 = 0U;
uint8 count3 = 0U;
uint8 count4 = 0U;
void test(void)
{
...
7
votes
3
answers
2k
views
Does uint16_t get promoted to int? And is it safe to typecast uint16_t to uint32_t?
uint32_t a = 10;
uint16_t b = 0;
if (a > b)
{
printf("a > b\n");
}
I was expecting that b would get promoted to int and the compiler would complain about comparing signed and ...
-3
votes
2
answers
328
views
Why is it not allowed to assign double literal to variable of type float? [duplicate]
In java, the compiler throws an error if you try to assign a double literal to an int variable. However, it allows the assignment of an int literal to variable of type long.
double d = 4.0;
...
3
votes
1
answer
78
views
Effect of default argument promotions on wchar_t
I am a bit confused about how default argument promotions effect wchar_t.
I understand that char is promoted to int, and therefore I have to supply int as the second parameter of va_arg, otherwise I ...
0
votes
2
answers
103
views
Same-line vs. multi-lined bitwise operation discrepancy
While writing a program for uni, I noticed that
unsigned char byte_to_write_1 = (0xFF << 2) >> 2; ==> 0xFF (wrong)
unsigned char byte_to_write_2 = (0xFF << 2);
byte_to_write_2 = ...
-1
votes
1
answer
132
views
Not able to compile code with unsigned and signed overloads of a function [duplicate]
I was trying to write an overloaded function to accept both signed and unsigned integers.
Following is my code:
#include <iostream>
void fun(const long long a)
{
std::cout << "...
22
votes
2
answers
3k
views
What is the result type of the bit shift operator?
Consider the following listing:
#include <type_traits>
#include <cstdint>
static_assert(std::is_same_v<decltype(31), int32_t>);
static_assert(std::is_same_v<decltype(31u), ...
5
votes
4
answers
718
views
How can I identify an integer promotion and a demotion in C?
I am learning C, and integer promotion and the term demotion is new to me.
I have read in the C Standard (C17) about the C type conversions and the integer promotion, but I don't know how to identify ...
3
votes
3
answers
237
views
Questions about C strlen function
I tried to compare with strlen(string) with -1 but different methods gave different results:
char string[] = {"1234"};
int len = strlen(string);
int bool;
bool = -1 < strlen(string);
...
6
votes
3
answers
245
views
C++ Bitshift in one line influenced by processor bit width (Bug or Feature?)
I encountered a strange problem, but to make it clear see the code first:
#include <stdio.h>
#include <stdint.h>
int main() {
uint8_t a = 0b1000'0000; // -> one leftmost bit
...
0
votes
1
answer
171
views
Usage of int instead of char [duplicate]
I've got a copy of the book "The C Programming Language", and in one of the first chapters it shows a short example of a code that copies characters from stdin to stdout. Here is the code:
...
1
vote
2
answers
148
views
Fixed point arithmetic long long int representation issue in C
I am struggling with the realization of a signed long long int variable having the value 1 set in its integer part.
Looking at the 16.16 implementation with a signed long int variable it does work ...
-1
votes
1
answer
57
views
Difference between the num as char and as int on memory - Regarding the equivalent num on the ascii - [duplicate]
What's the Difference between the number as char and the number as int (or any type, which I can make any arithmetic operation using it like double on c++) on memory - Regarding the equivalent number ...
10
votes
2
answers
1k
views
Is left shifting unsigned int more than its bit field width, but less than its type size undefined?
struct Type {
uint8_t var : 3;
};
int main()
{
struct Type bar;
bar.var = 1;
uint8_t baz = bar.var << 5;
}
According to the standard, left shifting more than the width of the ...
0
votes
1
answer
99
views
What is the result type of operation between integer literals?
int main(){
char a = 5 + (16711935 * 1200);
return 0;
}
Based on the type of integer literals and conversion rules in C, 16711935 is of type int and 1200 is promoted to an int. My question is what ...
3
votes
3
answers
992
views
Passing enum to argument of integral type
Consider the following code:
enum ABC : char
{
a, b, c
};
void ff(char c)
{
cout << "char\n";
}
void ff(int i)
{
cout << "int\n";
}
int main()
{
ff(a)...
0
votes
2
answers
365
views
How does type conversion and integer promotion work for stdint.h?
In C, I understand type conversions, integer promotion, casting, etc. for standard types, but how do the stdint.h types factor into this?
For type rankings, the rules state:
No two signed integer ...
0
votes
4
answers
193
views
what is the output of conditional operator with unary operator
I have the following code where behavior is not clear to me. Can some one please help how conditional operator evaluate the following code and output ans as 1
#include
int main() {
bool ...
7
votes
1
answer
466
views
Is relying on integer promotion a bad programming practice?
I'm currently writing some code for embedded systems (both in c and c++) and in trying to minimize memory use I've noticed that I used a lot of code that relies on integer promotions. For example (to ...
4
votes
3
answers
537
views
Why do `(char)~0` and `(unsigned char)~0` return values of different widths?
I bumped into this while writing a program trying to print the constituent byte values of UTF-8 characters.
This is the program that I wrote to test the various ~0 operations:
#include <stdio.h>
...
1
vote
2
answers
149
views
Why the sequence from the bitwise operator(~) would be this? Is that broken?
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned char a=100,b=50;
printf("%d & %d = %d\n",a,b,a&b);
printf("%d | %d = %d\n",a,b,a|b);
printf("...
2
votes
5
answers
8k
views
What does *= do?
Hey I am kinda new to C and I wanted to ask why this prints out 4 instead of 260?
#include <stdio.h>
int main()
{
unsigned char x = 130;
x *= 2;
printf("%d\n", x);
}
2
votes
2
answers
98
views
Question about Bitwise Shift in Microsoft C++ [duplicate]
I am doing the following bitwise shift in Microsoft C++:
uint8_t arr[3] = {255, 255, 255};
uint8_t value = (arr[1] << 4) >> 4;
The result of these operations confused me quite a bit:
...
-1
votes
2
answers
98
views
What is wrong with this code? The answer should be 24 for this question right? [closed]
What is wrong with this code? The answer should be 24 for this question right?
int t;
vector<int>arr={24,434};
for(int i=arr.size()-1;i>=(arr.size()-2);i--)
{
t=i;
}
cout<<...
3
votes
3
answers
155
views
Why is the expression true after bit shifting the value and condition with && in C [closed]
Have a look at the example:
unsigned char c = 64; /* 0100 0000 */
c = (c << 2 && 512); /* 512: 10 0000 0000 */
If I shift c two times to the left, I should get from 0100 0000 (64) to ...
4
votes
1
answer
299
views
(Where) does the C standard define the result of adding/subtracting two booleans?
The C11 standard defines the _Bool type (6.2.5.2) as a standard unsigned integer type (6.2.5.6) and as I read the standard, _Bool is then also an arithmetic type (6.2.5.18 via 6.2.5.7 and 6.2.5.17).
...
0
votes
1
answer
380
views
avoiding array index promotion in hopes of better performance
I have a huge array of integers and those integers are not greater than 0xFFFF. Therefore I would like save some space and store them as unsigned short.
unsigned short addresses[50000 /* big number ...
-1
votes
3
answers
139
views
C language printf function and output problem
I need help solving this problem in my mind, so if anyone had a similar problem it would help me a lot.
signed char c=0x10;
printf("%x", c<<0x4|c>>0x4);
Why output is 101?
0
votes
3
answers
1k
views
Printf in unsigned short int and unsigned int using %d
Why in second printf when using %d I am not getting -1?
#include<stdio.h>
int main(){
unsigned int u=-1;
unsigned short int y=-1;
printf("%d %u\n",u,u);
printf("%d ...
1
vote
3
answers
2k
views
Why does function return bool when it is defined as an int?
Why does the int hum2() function returning a boolean type? Shouldn't the function return the type I defined it as. Like Float function returns float value of Double function returns a double value.
#...