Let's look at Expert C Programming and see a detailed description of "type upgrade" in C language,
Type conversions in C are much more widespread than is generally realized. They can also occur in
Any expression that involves a type smaller than int or double. Take the following code,
Example:
Printf ("% d", sizeof 'A ');
The code prints out the size of the type that holds a character literal. Surely this will be the size of
Character, and hence "1 "? Try running the code. You will see you actually get "4" (or whatever size
Int is on your system). Character literals have type int and they get there by following the rules
Promotion from type char. This is too briefly covered in K & R 1, on page 39 where it says:
Every char in an expression is converted into an int .... Notice that all float's in an expression are
Converted to double .... Since a function argument is an expression, type conversions also take place
When arguments are passed to functions: in particle, char and short become int, float
Becomes double.
-The C Programming Language, first edition
The feature is known as type promotion. When it happens to integer types it's called "integral
Promotion ". The concept of automatic type promotion carried over to ansi c, although it was watered down in places.
The ansi c standard regulates the type promotion of C.
In executing the fragment
Char c1, c2;
/*...*/
C1 = c1 + c2;
The "integral promotions" require that the abstract machine promote the value of each variable to int
Size and then add the two ints and truncate the sum. Provided the addition of two chars can be done
Without creating an overflow exception, the actual execution need only produce the same result,
Possibly omitting the promotions.
Similarly, in the fragment
Float f1, f2;
Double d;
/*...*/
F1 = f2 * d;
The multiplication may be executed using single-precision arithmetic if the implementation can
Ascertain that the result wocould be the same as if it were executed using double-precision arithmetic
(For example, if d were replaced by the constant 2.0, which has type double ).
-Ansi c Standard, Section 5.1.2.3
Table 8-1 provides a list of all the usual type promotions. These occur in every expression, not just in expressions involving operators and mixed-type operands.
Table 8-1. Type Promotions in C
Original Type Usually Promoted
Char int
Bit-field int
Enum int
Unsigned char int
Short int
Unsigned short int
Float double
Array of anything pointer to anything
The integral promotions are: char, short int and bit-field types (and signed or unsigned versions
Of these), and enumeration types, will be promoted to int if they can be represented as such.
Otherwise they are promoted to unsigned int. ansi c says that the promotion doesn't have to be
Done if the compiler can guarantee that the same result occurs without it-this usually means a literal
Operand.
Note: All mentioned above are ansi c standards, which are different in C ++. For example, the above example: printf ("% d", sizeof 'A ');
For testing with VS 2010 and g ++, the output is 1, while for GCC, the output is 4.
For the type conversion in C language, there are several good answers on stackoverflow:
Size of character ('A') in C/C ++
Integral promotion
In a C expression where unsigned int and signed int are present, which type will be promoted to what type?