Questions tagged [integer-promotion]

Anything related to C and C++ integer promotions, i.e. a class of data-type conversions that happens automatically when an object of integer type appears in certain contexts (e.g. when a value of type `short` is added to an `int` it is automatically promoted to `int` type before performing the operation).

In C and C++ integer promotion refers to automatic type conversions made between compatible integral types. When an operation is attempted between two compatible types (one can be safely converted into the other) any necessary adjustments are added silently by the compiler. This helps to avoid type casting where the programmer's intent is clear.

239 questions
7
votes
2 answers

C++ ambiguous call to overloaded function with unsigned int

This seems inconsistent. I have 3 functions f overloaded for signed types short, int and long long. If you pass an unsigned short then it gets promoted to the next biggest signed type int. However if you pass unsigned int then it doesn't get…
George Skelton
  • 1,095
  • 1
  • 10
  • 22
7
votes
4 answers

Confusion about sizeof operator in C

I'm confused about sizeof operator in C. #include int main(void) { char num1=1, num2=2, result; result = num1 + num2; printf("Size of result: %d \n",sizeof result); printf("Size of result: %d \n",sizeof(num1+num2)); } The…
Jin
  • 1,902
  • 3
  • 15
  • 26
7
votes
3 answers

C++ - Bit-wise not of uchar produces int

I am surprised by C++'s behavior when applying bit-wise not to an unsigned char. Take the binary value 01010101b, which is 0x55, or 85. Applying bit-wise not on an eight bit representation should yield 10101010b, which is 0xAA, or 170. However, I…
Lemming
  • 4,085
  • 3
  • 23
  • 36
7
votes
1 answer

Integer promotion, signed/unsigned, and printf

I was looking through C++ Integer Overflow and Promotion, tried to replicate it, and finally ended up with this: #include #include using namespace std; int main() { int i = -15; unsigned int j = 10; cout << i+j <<…
rainer
  • 6,769
  • 3
  • 23
  • 37
7
votes
1 answer

Integral promotion

When is it ever the case that a signed integer cannot represent all the values of the original type with regards to integer promotion? From the text K&R, C Programming Language, 2nd Ed. p. 174 A.6.1 Integral Promotion A character, a short integer,…
Frank Vilea
  • 8,323
  • 20
  • 65
  • 86
6
votes
0 answers

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 result is truncated and stored back to v. With…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
6
votes
2 answers

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 complain that the function has already been…
arye
  • 458
  • 3
  • 15
6
votes
3 answers

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 #include int main() { uint8_t a = 0b1000'0000; // -> one leftmost bit uint8_t b = 0b1000'0000; a = (a << 1) >> 1; // -> Both…
6
votes
1 answer

Understanding 2^31 and -2^31 integer promotion

#include int main() { printf("sizeof(int): %zu\n", sizeof(int)); printf("%d\n", 2147483648u > -2147483648); printf("%d\n", ((unsigned int)2147483648u) > ((int)-2147483648)); printf("%d\n", 2147483648u != -2147483648); …
Irfy
  • 9,323
  • 1
  • 45
  • 67
6
votes
4 answers

Why does unary minus perform integral promotion?

const auto min = -std::numeric_limits::max(); T x = min; // conversion from 'const int' to 'short', possible loss of data T is a template argument, a short in this case. Unary minus apparently performs integral promotion. Why does unary minus…
David
  • 27,652
  • 18
  • 89
  • 138
5
votes
4 answers

storing signed short in the lower 16 bits of a an unsigned int

I'm programming C on an embedded system. The processor architecture is 32 bits (sizeof(int) is 32 bits, sizeof(short) is 16 bits). There is a 32-bit variable that is a memory-mapped control register (CTRL_REG) that is specified as only the bottom…
rmeador
  • 25,504
  • 18
  • 62
  • 103
5
votes
5 answers

Truncating an int to char - is it defined?

unsigned char a, b; b = something(); a = ~b; A static analyzer complained of truncation in the last line, presumably because b is promoted to int before its bits are flipped and the result will be of type int. I am only interested in the last byte…
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
5
votes
1 answer

Integer promotion example in C

void foo(void) { unsigned int a = 6; int b = -20; if (a+b > a) { printf("> a"); } else { printf("< a"); } } I am trying to understand what is going on with the integer promotion example above. I know that for a…
royK
  • 73
  • 5
5
votes
5 answers

If char c = 0x80, why does printf("%d\n", c << 1) output -256?

#include int main(void) { char c = 0x80; printf("%d\n", c << 1); return 0; } The output is -256 in this case. If I write c << 0 then the output is -128. I don't understand the logic behind this code.
Brite Roy
  • 425
  • 3
  • 9
  • 28
5
votes
2 answers

Integer promotion in C program

I have written a simple C program and wanted to know if integer promotion is happening in it. Please explain how integer promotion happens and how to avoid it? /* start of main */ unsigned short int res; unsigned short int bsp; signed short int…
Ravi
  • 69
  • 3