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
21
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,187
  • 3
  • 28
  • 46
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
  • 25,830
  • 16
  • 80
  • 130
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,870
  • 1
  • 17
  • 27
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
16
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; …
15
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
  • 195,354
  • 31
  • 331
  • 669
15
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,310
  • 2
  • 20
  • 31
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
  • 428,875
  • 83
  • 813
  • 1,025
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…
deceleratedcaviar
  • 4,619
  • 2
  • 36
  • 61
13
votes
2 answers

Explain integer comparison with promotion

I'm trying to understand how integer promotion and comparison in and 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
  • 131
  • 2
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
  • 100,604
  • 94
  • 332
  • 495
13
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
  • 35,042
  • 4
  • 82
  • 127
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,103
  • 7
  • 13
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
  • 377
  • 3
  • 14
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,463
  • 3
  • 10
  • 13
1
2
3
12 13