C 类型说明符C Type Specifiers

声明中的类型说明符定义变量或函数声明的类型。Type specifiers in declarations define the type of a variable or function declaration.

语法Syntax

type-specifiertype-specifier:
voidvoid

charchar

shortshort

intint

longlong

floatfloat

doubledouble

signedsigned

unsignedunsigned

struct-or-union-specifierstruct-or-union-specifier

enum-specifierenum-specifier

typedef-nametypedef-name

signed char、signed int、signed short int 和 signed long int 类型与其 unsigned 对等项和 enum 一起称作“整型”类型。The signed char, signed int, signed short int, and signed long int types, together with their unsigned counterparts and enum, are called "integral" types. float、double 和 long double 类型说明符称作“浮动”或“浮点”类型。The float, double, and long double type specifiers are referred to as "floating" or "floating-point" types. 可在变量或函数声明中使用任何整型或浮点型说明符。You can use any integral or floating-point type specifier in a variable or function declaration. 如果声明中未提供 type-specifier,则将它用作 intIf a type-specifier is not provided in a declaration, it is taken to be int.

可选关键字 signed 和 unsigned 可位于任何整型类型的前面或后面(enum 除外),也可将其单独用作类型说明符(在这种情况下,它们分别被理解为 signed int 和 unsigned int)。The optional keywords signed and unsigned can precede or follow any of the integral types, except enum, and can also be used alone as type specifiers, in which case they are understood as signed int and unsigned int, respectively. 单独使用时,关键字 int 被假定为 signed。When used alone, the keyword int is assumed to be signed. 单独使用时,关键字 long 和 short 被理解为 long int 和 short intWhen used alone, the keywords long and short are understood as long int and short int.

枚举类型被视为基本类型。Enumeration types are considered basic types. 枚举声明中讨论了枚举类型的类型说明符。Type specifiers for enumeration types are discussed in Enumeration Declarations.

关键字 void 有三种用途:指定函数返回类型、为未采用参数的函数指定一个参数类型列表以及指定一个指向未指定的类型的指针。The keyword void has three uses: to specify a function return type, to specify an argument-type list for a function that takes no arguments, and to specify a pointer to an unspecified type. 可以使用 void 类型声明不返回值的函数或声明指向未指定的类型的指针。You can use the void type to declare functions that return no value or to declare a pointer to an unspecified type. 有关 void 单独出现在括号中且跟在函数名称后时的相关信息,请参阅参数See Arguments for information on void when it appears alone within the parentheses following a function name.

Microsoft 专用Microsoft Specific

类型检查现在是 ANSI 兼容的,这意味着 short 类型和 int 类型是不同类型。Type checking is now ANSI-compliant, which means that type short and type int are distinct types. 例如,下面是编译器的早期版本接受的 Microsoft C 编译器中的一个重新定义。For example, this is a redefinition in the Microsoft C compiler that was accepted by previous versions of the compiler.

int   myfunc();  
short myfunc();  

下一个示例还会生成有关到不同类型的间接寻址的警告:This next example also generates a warning about indirection to different types:

int *pi;  
short *ps;  

ps = pi;  /* Now generates warning */  

Microsoft C 编译器还生成保留符号的差异警告。The Microsoft C compiler also generates warnings for differences in sign. 例如:For example:

signed int *pi;  
unsigned int *pu  

pi = pu;  /* Now generates warning */  

为副作用计算类型 void 表达式。Type void expressions are evaluated for side effects. 无论如何,不能使用具有 void 类型的表达式的值(不存在),也不能将 void 表达式转换为除 void 以外的任何类型(通过隐式或显式转换)。You cannot use the (nonexistent) value of an expression that has type void in any way, nor can you convert a void expression (by implicit or explicit conversion) to any type except void. 如果您在需要 void 表达式的上下文中确实使用了任何其他类型的表达式,则其值将被丢弃。If you do use an expression of any other type in a context where a void expression is required, its value is discarded.

为了符合 ANSI 规范,void** 不能用作 int**。To conform to the ANSI specification, void*\* cannot be used as int*\*. 仅 void* 可用作指向未指定的类型的指针。Only void\* can be used as a pointer to an unspecified type.

结束 Microsoft 专用END Microsoft Specific

可以使用 typedef 声明创建附加类型说明符,如 Typedef 声明中所述。You can create additional type specifiers with typedef declarations, as described in Typedef Declarations. 有关每个类型的大小的信息,请参阅基本类型的存储See Storage of Basic Types for information on the size of each type.

另请参阅See Also

声明和类型Declarations and Types