Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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** */
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
like image 278
Lal0ver Avatar asked Oct 02 '15 14:10

Lal0ver


People also ask

What is type promotion in C?

Type promotion in C is a method to convert any variable from one datatype to another. C allows variables of different datatypes to be present in a single expression. There are different types of type conversions available in C. They are Implicit type conversion and Explicit type conversion.

What type of variable is ch in C++?

The variable ch is of type char and first promoted to type int (integer promotions) and then promoted to type double (balancing). The result of 2.0 * ch is of type double.

What is integer promotion in C99?

Note: In C99, integer promotion is clearly defined in the following rule (6.3.1.1): “If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

What is the type of the variable 2 * Ch?

The variable ch is of type char and promoted to type int (integer promotions). The result of 2 * ch is of type int and promoted to type float (balancing).


1 Answers

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++?.

like image 81
Shafik Yaghmour Avatar answered Sep 23 '22 08:09

Shafik Yaghmour