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
12
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,700
  • 1
  • 19
  • 49
11
votes
2 answers

Data type promotions during arithmetic operations: -1 < (unsinged int) 1 == false

main() { if ( -1 < (unsigned char) 1 ) printf("less than"); else printf("NOT less than"); } Prints less than. Because, (unsigned char) 1 is converted to (signed char) 1 and then: (signed) -1 < (signed) 1, thus output is…
bakra
  • 387
  • 4
  • 15
11
votes
3 answers

Which integral promotions do take place when printing a char?

I recently read that unsigned char x=1; printf("%u",x); invokes undefined behaviour since due to the format specifier %u, printf expects an unsigned int. But still I would like to understand what is going on in this example. I think that the…
lee77
  • 1,493
  • 3
  • 10
  • 14
11
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
  • 78,592
  • 17
  • 171
  • 225
10
votes
2 answers

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 left operand type is undefined behavior: 6.5.7 Bitwise…
Iman
  • 348
  • 2
  • 11
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
  • 12,299
  • 15
  • 75
  • 115
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
  • 332
  • 2
  • 17
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
  • 80,836
  • 20
  • 110
  • 183
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
  • 42,792
  • 11
  • 94
  • 169
9
votes
1 answer

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 - subtrahend result_plus = minuend +…
bers
  • 4,817
  • 2
  • 40
  • 59
9
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
  • 740
  • 4
  • 15
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

Conversion warning when adding two uint8_t

I have the following C code: typedef unsigned char uint8_t; void main(void) { uint8_t a = 1, b = 2, res; res = a + b; } When I compile this code using gcc -Wconversion, I get the following warning: test.c: In function…
watain
  • 4,838
  • 4
  • 34
  • 35
1 2
3
15 16