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
23
votes
3 answers

Addition of two chars produces int

I've made a simple program and compiled it with GCC 4.4/4.5 as follows: int main () { char u = 10; char x = 'x'; char i = u + x; return 0; } g++ -c -Wconversion a.cpp And I've got the following: a.cpp: In function ‘int main()’: a.cpp:5:16:…
Rom098
  • 2,445
  • 4
  • 35
  • 52
23
votes
6 answers

Why is unsigned short (multiply) unsigned short converted to signed int?

Why is unsigned short * unsigned short converted to int in C++11? The int is too small to handle max values as demonstrated by this line of code. cout << USHRT_MAX * USHRT_MAX << endl; overflows on MinGW 4.9.2 -131071 because (source) USHRT_MAX =…
Slazer
  • 4,750
  • 7
  • 33
  • 60
22
votes
2 answers

What is the result type of the bit shift operator?

Consider the following listing: #include #include static_assert(std::is_same_v); static_assert(std::is_same_v); static_assert(std::is_same_v
ivaigult
  • 6,198
  • 5
  • 38
  • 66
22
votes
4 answers

What is going on with bitwise operators and integer promotion?

I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size. #include #include #include int main() { uint8_t x = 12; std::cout << (x << 1) << '\n'; std::cout << ~x; …
20
votes
1 answer

Why isn't common_type::type = long long?

common_type::type is unsigned long because concerning the operands after integral promotion the standard says... [...] if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of…
David
  • 27,652
  • 18
  • 89
  • 138
19
votes
2 answers

Why auto is deduced to int instead of uint16_t

I have the following code: uint16_t getLastMarker(const std::string &number); ... const auto msgMarker = getLastMarker(msg.number) + static_cast(1); static_assert(std::is_same::value, "Should…
VestniK
  • 1,910
  • 2
  • 17
  • 29
17
votes
4 answers

Does Unary + operator do type conversions?

Till now I was believing that there is no use of unary + operator. But then I came across with following example: char ch; short sh; int i; printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i)); // output: 1 2 4 printf("%d %d…
A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32
16
votes
8 answers

Would making plain int 64-bit break a lot of reasonable code?

Until recently, I'd considered the decision by most systems implementors/vendors to keep plain int 32-bit even on 64-bit machines a sort of expedient wart. With modern C99 fixed-size types (int32_t and uint32_t, etc.) the need for there to be a…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
16
votes
1 answer

Variable promotion in C

I'm having a problem figuring out why the output is different in each of these particular cases. In the sample Code a, there is a variable promotion as I expect and the result it's > 6, but in the sample Code b, the result is <= 6: /* **Code a**…
Lal0ver
  • 193
  • 5
15
votes
2 answers

Is char default-promoted?

This may be a silly question, but could someone please provide a standard reference for C++11 and C11: Is char default-promoted to int? Here's a little background: Both C and C++ have notions of default argument promotion (C++11: 5.2.2/7; C11:…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
14
votes
3 answers

Why don't I get an integer overflow when adding two chars?

Possible Duplicate: Addition of two chars produces int Given the following C++ code: unsigned char a = 200; unsigned char b = 100; unsigned char c = (a + b) / 2; The output is 150 as logically expected, however shouldn't there be an integer…
hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
14
votes
2 answers

Explain integer comparison with promotion

I'm trying to understand how integer promotion and comparison in a c++ application works. #include int main(void) { uint32_t foo = 20; uint8_t a = 2; uint8_t b = 1; uint8_t c = 5; if(foo == b*c) {} if(foo ==…
Ralf
  • 141
  • 3
14
votes
5 answers

Inconsistent behaviour of implicit conversion between unsigned and bigger signed types

Consider following example: #include int main(void) { unsigned char a = 15; /* one byte */ unsigned short b = 15; /* two bytes */ unsigned int c = 15; /* four bytes */ long x = -a; /* eight bytes */ printf("%ld\n",…
Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
13
votes
1 answer

Yoda Conditions and integer promotion

When comparing a type larger than int, with an integer constant, should I place the constant on the left or the right to ensure the correct comparison is performed? int64_t i = some_val; if (i == -1) or should it be: if (-1 == i) Are there any…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
12
votes
2 answers

Can an unsigned integer addition invoke undefined behavior?

Edit: Changed the value for USHRT_MAX as it was not conformant as shown by comments. Imagine you have a fancy compiler where its integer type limits, as defined in limits.h are: #define INT_MAX 2147483647 /* Typical 32-bit system value…
atturri
  • 1,133
  • 7
  • 13
1
2
3
15 16