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.

193 questions
10
votes
3 answers

Why do implementations of "stdint.h" disagree on the definition of UINT8_C?

The UINT8_C macro is defined in "stdint.h", with the following specification: The macro UINTN_C(value) shall expand to an integer constant expression corresponding to the type uint_leastN_t. In the wild, however, implementations differ: #define…
Clément
  • 10,212
  • 13
  • 62
  • 104
10
votes
3 answers

Portable way to retrieve a int32_t passed to variadic function

7.16.1.1 2 describes va_arg as following (emphasis mine): If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the…
10
votes
1 answer

Implicit conversion: is the following warning valid?

This question Implicit type conversion rules in C++ operators (and several others) state If either is long long unsigned int the other is promoted to long long unsigned int However if I do the following under MSVC: unsigned int a =
10
votes
1 answer

Deterministic way of saying "promote everything to floating before calculation" in C++

Given that I'd prefer to keep numbers in my program as ints or whatever integral, what is the most convenient way of doing an arbitrary arithmetic with floating point equivalents of those numbers? Say, I have int a,b,c,d; double x; And I want to…
Euri Pinhollow
  • 322
  • 2
  • 16
10
votes
1 answer

Why is (int64_t)-1 + (uint32_t)0 signed?

Why is (int64_t)-1 + (uint32_t)0 signed in C? It looks like it's int64_t, but my intuition would say uint64_t. FYI When I run #include #include #define BIT_SIZE(x) (sizeof(x) * 8) #define IS_UNSIGNED(x) ((unsigned)(((x) * 0 -…
pts
  • 64,123
  • 15
  • 92
  • 159
10
votes
2 answers

Integer promotion with the operator <<

Similar to the question Bitshift and integer promotion?, I have a question about integer promotion when using left bitshifts. unsigned int test(void) { unsigned char value8; unsigned int result; value8 = 0x12; result = value8 << 8; …
jeb
  • 70,992
  • 15
  • 159
  • 202
9
votes
2 answers

Using low bitsize integral types like `Int8` and what they are for

Recently I've learned that every computation cycle performs on machine words which on most contemporary processors and OS'es are either 32-bit or 64-bit. So what are the benefits of using the smaller bit-size values like Int16, Int8, Word8? What are…
Nikita Volkov
  • 41,289
  • 10
  • 85
  • 162
9
votes
1 answer

Should bit-fields less than int in size be the subject of integral promotion?

Let's say I have following struct: struct A { unsigned int a : 1; unsigned int b : 1; }; What interests me is the type of expression a + b. While technically bit-fields have "type" with size less than int so integral promotion probably…
9
votes
3 answers

Do integer promotions always happen when evaluating an expression?

Possible Duplicate: C integer overflow behaviour when assigning to larger-width integers I haven't found a clear answer on this in my googling. Say you have two expressions: int16_t a16 = 100; int16_t b16 = 2000; int16_t result16 = (a16 * b16) /…
Cornstalks
  • 34,193
  • 13
  • 69
  • 132
8
votes
2 answers

What is (INT32_MIN + 1) when int32_t is an extended integer type and int is 32-bit one's complement standard integer type

Imagine this situation. int32_t is an extended integer type and it's represented in two's complement (as the standard required int32_t to be represented). This means that INT32_MIN is -2147483648 (0x80000000). Meanwhile int is a standard integer…
MarkWeston
  • 692
  • 3
  • 15
8
votes
2 answers

Does multiplying unsigned short cause undefined behaviour?

As a follow-up to "https://stackoverflow.com/questions/33732041/why-static-castunsigned-intushrt-maxushrt-max-yields-correct-value" I was asking myself if promoting all types (except some exceptions) with a lower rank than int to int to perform…
Simon Kraemer
  • 5,013
  • 1
  • 16
  • 43
8
votes
4 answers

What is the difference between literals and variables in C (signed vs unsigned short ints)?

I have seen the following code in the book Computer Systems: A Programmer's Perspective, 2/E. This works well and creates the desired output. The output can be explained by the difference of signed and unsigned representations. #include int…
8
votes
6 answers

Why does C/C++ automatically convert char/wchar_t/short/bool/enum types to int?

So, if I understood it well, integral promotion provides that: char, wchar_t, bool, enum, short types ALWAYS are converted to int (or unsigned int). Then, if there are different types in an expression, further conversion will be applied. Am I…
user2148758
  • 303
  • 2
  • 6
8
votes
2 answers

Is unsigned char always promoted to int?

Suppose the following: unsigned char foo = 3; unsigned char bar = 5; unsigned int shmoo = foo + bar; Are foo and bar values guaranteed to be promoted to int values for the evaluation of the expression foo + bar -- or are implementations allowed to…
Vilhelm Gray
  • 10,078
  • 8
  • 55
  • 108
8
votes
3 answers

Integer promotion - what are the steps

This code prints B2 short a=-5; unsigned short b=-5u; if(a==b) printf("A1"); else printf("B2"); I read about integer promotion but it's still unclear to me, how does it work in the example here? Can someone thoroughly post the steps the…
Johnny Pauling
  • 10,391
  • 14
  • 57
  • 104
1 2
3
12 13