Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are chars automatically promoted in C expressions?

I made a statement to a colleague of mine, which was:

"chars are automatically promoted to integers in C expressions, and that's fine for performance since CPUs work fastest with their natural word size.

I believe char promotion behavior is stated somewhere in the standard due to a char's rank.

This is the response I got back:

"Characters are not default promoted to an integer. The register size is 32 bit, but multiple byte values in a row can be packed into a single register as a compiler implementation. This is not always predictive. The only time you can verify automatic promotion is when the type is passed into the call stack when not wrapped around a structure because C standard officially needs 32-bit values in the call stack memory. A great deal of CPU architectures have optimized assembly calls for non-32 bit values, so no assumptions can be made about the CPU or compiler in this case."

I'm not sure who is right, and what to believe. What are the facts?

like image 680
Trevor Hickey Avatar asked Dec 09 '22 01:12

Trevor Hickey


2 Answers

chars are automatically promoted to integers in C expressions

Yes, they are. C99 section 6.3.1.8, Usual arithmetic conversions:

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:

  • First, if the corresponding real type of either operand is long double, the other operand is converted, without change of type domain, to a type whose corresponding real type is long double.
  • Otherwise, if the corresponding real type of either operand is double, the other operand is converted, without change of type domain, to a type whose corresponding real type is double.
  • Otherwise, if the corresponding real type of either operand is float, the other operand is converted, without change of type domain, to a type whose corresponding real type is float.62)
  • Otherwise, the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
    • If both operands have the same type, then no further conversion is needed.
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
    • 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.
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
    • Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

Integer promotions are described on Section 6.3.1.1.2:

The following may be used in an expression wherever an int or unsigned int may be used:

  • An object or expression with an integer type whose integer conversion rank is less than or equal to the rank of int and unsigned int.
  • A bit-field of type _Bool, int, signed int, or unsigned int

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 unchanges by the integer promotions.

The rank of a char is less than or equal to that of an int, so char is included in here.

(As a footnote, it is mentioned that integer promotions are only applied as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, - and ~, and to both operands of the shift operators).

As mentioned in the comments, integer promotion is also performed on function-call arguments.

like image 135
Filipe Gonçalves Avatar answered Dec 20 '22 08:12

Filipe Gonçalves


Yes, expressions with multiple chars, like addition etc.etc. (but not stuff like the comma operator), and some other things, are done on promoted values (promoted to int). See eg. N3797, §4.5

About the statement of your colleague, there are many wrong things in it:

  • A "registry" (register) size is not generally 32 bit, not at all.

  • If a byte has 8 bit, of course a register with 32 bit can hold multiple bytes,
    but this isn´t relevant, and the compiler is not the reason why it is possible.

  • What about this is "predictive"?

  • The bit about the standard and 32 bit is completely wrong.

  • Integer promotion has nothing to do with struct

  • In the standard, there is no "stack". That the concept
    of a stack is used in reality is not mandatory (as others said).

  • He´s saying that everything needs to be 32 bit, but as CPUs
    could process other sizes too, nothing can be said for sure? What now?

...

like image 29
deviantfan Avatar answered Dec 20 '22 07:12

deviantfan