看 Expert C Programming 看到了关于C语言中“类型提升”的详细说明,
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, for
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 a
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 for
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 particular, 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.
ANSI C 标准对C语言的“类型提升” (type promotion)作了规范
In executing the fragment注意:上面所提及的都是ANSI C标准,在C++中情况是不同的,比如上面的例子: printf(" %d ", sizeof 'A' );
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 would 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.3Table 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 To
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.
用VS 2010 和g++测试的话输出为1,而使用GCC的话为4。
关于C语言中的类型转换,在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?