设为首页 加入收藏

TOP

关于C语言中的 " 类型提升 "(type promotion)
2014-11-23 22:04:08 】 浏览:7378
Tags:关于 言中 " 类型 提升 type promotion

看 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
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.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 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.

注意:上面所提及的都是ANSI C标准,在C++中情况是不同的,比如上面的例子:printf(" %d ", sizeof 'A' );
用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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c语言编程之sglib库的简单使用 下一篇C语言课程设计――学生学籍管理系..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目