程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 關於C語言中的 " 類型提升 "(type promotion)

關於C語言中的 " 類型提升 "(type promotion)

編輯:關於C語言

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

 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved