16

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** */
puts("Code a\n");
unsigned int a = 6;
int b = -20;
( a+b > 6) ? puts("> 6\n") : puts("<= 6\n");

/* **Code b** */
puts("Code b:\n");
uint8_t a1 = 6;
int8_t  b1 = -20;  
( a1+b1 > 6) ? puts("> 6\n") : puts("<= 6\n");

Output:

Code a

> 6

Code b:

<= 6
nobody
  • 7,803
  • 11
  • 56
  • 91
Lal0ver
  • 193
  • 5
  • Take a look on : [In a C expression where unsigned int and signed int are present, which type will be promoted to what type?](http://stackoverflow.com/questions/2280663/in-a-c-expression-where-unsigned-int-and-signed-int-are-present-which-type-will) I hope that will help you. – Missu Oct 02 '15 at 14:47
  • Providing an image for textual output? That is automatically loaded when some opens this question? Do you have a tracker behind that ?) Please just use cut and paste in such cases. – Jens Gustedt Oct 02 '15 at 15:39
  • 5
    A fine example of a basic, yet well formed post: Clear title, clear difficulty statement, sample code, output, expected output, good tags. – chux - Reinstate Monica Oct 02 '15 at 16:49
  • 1
    @chux, I wouldn't personally call C's promotion and UAC rules basic, but your point stands. – chris Oct 02 '15 at 20:50

1 Answers1

9

The usual arithmetic conversions are performed on the operands of addition. For integer types, this consists of the integer promotions if needed, and if the two operands do not have the same type a further conversion is done to bring them to a common type.

In the first case there are no promotions but the int operand is converted to unsigned int because int can not hold all the possible values of unsigned int.

In the second case both operands are promoted to int and stay as an int since they have a common type.

For reference the draft C11 standard in section 6.5.6 Additive operators says:

If both operands have arithmetic type, the usual arithmetic conversions are performed on them.

section 6.3.1.8 Usual arithmetic conversions says:

Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result. For the specified operands, each operand is converted, without change of type domain, to a type whose corresponding real type is the common real type. Unless explicitly stated otherwise, the common real type is also the corresponding real type of the result, whose type domain is the type domain of the operands if they are the same, and complex otherwise. This pattern is called the usual arithmetic conversions

[...]

Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands

[...]

  • Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type

[...]

A good reference for the rationale for this can be found in the question: Why must a short be converted to an int before arithmetic operations in C and C++?.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • It makes perfect sense now. Thanks – Lal0ver Oct 02 '15 at 14:59
  • Both the "integer promotions" and the conversions needed to bring the operands to a common type are part of the "usual arithmetic conversions". See [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) 6.3.1.8. – Keith Thompson Oct 02 '15 at 20:20
  • @KeithThompson hmmm, I did not mean for it to sound otherwise but I can see how it reads that way. – Shafik Yaghmour Oct 02 '15 at 20:22