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
4
votes
1 answer

uint8_t operations, when do they overflow?

I'm not sure when I have to worry about overflows when using unsigned chars. This case is clear: uint8_t a = 3; uint8_t b = 6; uint8_t c = a - b; // c is 253 However, what happens here: float d = a - b; // d is -3 Are both a and be converted to…
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
4
votes
3 answers

Promotion when evaluating constant integer expressions in preprocessor directives - GCC

NOTE: See my edits below. ORIGINAL QUESTION: Came across some curious behaviour which I cannot reconcile: #if -5 < 0 #warning Good, -5 is less than 0. #else #error BAD, -5 is NOT less than 0. #endif #if -(5u) < 0 #warning Good, -(5u) is less than…
joeymorin
  • 49
  • 2
4
votes
4 answers

What happens when a integer overflow occurs in a C expression?

I have the following C code: uint8_t firstValue = 111; uint8_t secondValue = 145; uint16_t temp = firstValue + secondValue; if (temp > 0xFF) { return true; } return false; This is the alternative implementation: uint8_t firstValue =…
4
votes
2 answers

Is `uint_fast32_t` guaranteed to be at least as wide as `int`?

The C standard specifies that integer operands smaller than int will be promoted to int before any arithmetic operations are performed upon them. As a consequence, operations upon two unsigned values which are smaller than int will be performed…
supercat
  • 77,689
  • 9
  • 166
  • 211
3
votes
0 answers

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; uint8_t output_B = 0x00; uint8_t example =…
wzs
  • 31
  • 2
3
votes
1 answer

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 may (GCC) or may not (MSVC) get an error, as…
z32a7ul
  • 3,695
  • 3
  • 21
  • 45
3
votes
3 answers

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); printf("%d",bool); //bool=0 bool = -1 < len; printf("%d",bool);…
3
votes
5 answers

Adding unsigned integers in C

Here are two very simple programs. I would expect to get the same output, but I don't. I can't figure out why. The first outputs 251. The second outputs -5. I can understand why the 251. However, I don't see why the second program gives me a…
user678392
  • 1,981
  • 3
  • 28
  • 50
3
votes
3 answers

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); // char } May I ask why complier matches ff(char) instead of ff(int)? I…
3
votes
3 answers

Why is the expression true after bit shifting the value and condition with && in C

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 this 0001 0000 0000 (256). So my 8-bit char c has only zeroes.…
B0r1
  • 400
  • 3
  • 15
3
votes
2 answers

macro constants with and without "u" at the end of a number

What is the difference between Usage #define CONSTANT_1 (256u) #define CONSTANT_2 (0XFFFFu) and #define CONSTANT_1 (256) #define CONSTANT_2 (0XFFFF) when do I really need to add u and what problems we get into if not? I am more interested in the…
IrAM
  • 1,720
  • 5
  • 18
3
votes
1 answer

Automatic promotion of arithmetic operations

Please see the following code: #include int main() { using T = short; auto x = T(1) + T(1); static_assert(std::is_same_v); } It seems that the above static_assert fails for all of gcc, clang, and msvc and I…
Junekey Jeon
  • 1,496
  • 1
  • 11
  • 18
3
votes
1 answer

Automatic conversions and promotions in function calls in C++

How to learn which are conversions and promotions in function calls?. From my previous question related to why are some functions called despite of others could have also been called too, i want to underline three keywords: conversions, promotions,…
Cătălina Sîrbu
  • 1,253
  • 9
  • 30
3
votes
3 answers

Why doesn't integer promotion happen in my case?

#include void main() { unsigned char a = 0xfb; char b = 0xfb; printf("case1 : %d, %d", a, b); // case1 char x=90, y=60, z=100, h; h = x*y/z; printf("\ncase2 : %d", h); // case2 int m = 32768, n = 65536,…
op ol
  • 705
  • 4
  • 11
3
votes
2 answers

Value initialization from signed char to integer, premature promotion?

In this piece of code: signed char v = -64; int n = 4; int x = v - '0' * (signed char)n; std::cout << x << std::endl; Should x be -5 or -261? In my understanding, the initializer expression has signed char type, and the type conversion should…
ABu
  • 10,423
  • 6
  • 52
  • 103