C
C 语法

Type

Type

(有关大多数内置类型的细节以及由C库提供的与类型相关的实用程序的列表,另请参阅算术类型)。

对象,函数和表达式都有一个名为type的属性,它决定了存储在对象中或由表达式求值的二进制值的解释。

类型分类

C型系统由以下类型组成:

  • 方式 void

对于上面列出的每种类型,可能存在几种其类型的合格版本,对应于const,volatile和restrict限定符(限定符语义所允许的)中的一个,两个或全部三个的组合。

类型组

  • 对象类型:不是函数类型的所有类型

兼容的类型

在C程序中,引用不同翻译单元中相同对象或函数的声明不必使用相同的类型。他们只需使用足够类似的类型,正式称为兼容类型。同样适用于函数调用和左值访问; 参数类型必须与参数类型兼容,并且左值表达式类型必须与所访问的对象类型兼容

类型TU兼容。

  • 它们是相同的类型(由typedef引入的同名或别名)

该类型char不兼容signed char且不兼容unsigned char

如果两个声明引用相同的对象或函数并且不使用兼容的类型,则程序的行为是未定义的。

// Translation Unit 1 struct S {int a;}; extern struct S *x; // compatible with TU2's x, but not with TU3's x // Translation Unit 2 struct S; extern struct S *x; // compatible with both x's // Translation Unit 3 struct S {float a;}; extern struct S *x; // compatible with TU2's x, but not with TU1's x // the behavior is undefined

// Translation Unit 1 #include <stdio.h> struct s {int i;}; // compatible with TU3's s, but not TU2's extern struct s x = {0}; // compatible with TU3's x extern void f(void // compatible with TU2's f int main() { f( return x.i; } // Translation Unit 2 struct s {float f;}; // compatible with TU4's s, but not TU1's s extern struct s y = {3.14}; // compatible with TU4's y void f() // compatible with TU1's f { return; } // Translation Unit 3 struct s {int i;}; // compatible with TU1's s, but not TU2's s extern struct s x; // compatible with TU1's x // Translation Unit 4 struct s {float f;}; // compatible with TU2's s, but not TU1's s extern struct s y; // compatible iwth TU2's y // the behavior is well-defined: only multiple declarations // of objects and functions must have compatible types, not the types themselves

注意:C ++没有兼容类型的概念。声明在不同翻译单元中兼容但不相同的两种类型的AC程序不是有效的C ++程序。

复合类型

不完整的类型

不完整的类型是一种对象类型,缺少足够的信息来确定该类型对象的大小。不完整的类型可能会在翻译单元的某个位置完成。

以下类型不完整:

  • 类型void。这种类型无法完成。

键入名称

类型可能必须在声明之外的上下文中命名。在这些情况下,使用类型名称,在语法上,它与类型说明符类型限定符列表完全相同,后面跟着声明符(参见声明),就像用来声明单个对象或函数一样类型,除了标识符被省略:

int n; // declaration of an int sizeof(int // use of type name int *a[3]; // declaration of an array of 3 pointers to int sizeof(int *[3] // use of type name int (*p)[3]; // declaration of a pointer to array of 3 int sizeof(int (*)[3] // use of type name int (*a)[*] // declaration of pointer to VLA (in a function parameter) sizeof(int (*)[*]) // use of type name (in a function parameter) int *f(void // declaration of function sizeof(int *(void) // use of type name int (*p)(void // declaration of pointer to function sizeof(int (*)(void) // use of type name int (*const a[])(unsigned int, ...) = {0}; // array of pointers to functions sizeof(int (*const [])(unsigned int, ...) // use of type name

除了标识符周围的多余括号在类型名称中有意义并且代表“没有参数规范的函数”:

int (n // declares n of type int sizeof(int () // uses type "function returning int"

类型名称用于以下情况:

  • cast

复合文字(自C99以来)
通用选择alignof alignas _Atomic(当用作类型说明符时)(自C11以来)

  • 复合文字

(since C99)

  • generic selection

(since C11)

类型名称可能会引入新类型:

void* p = (void*)(struct X {int i;} *)0; // type name "struct X {int i;}*" used in the cast expression // introduces the new type "struct X" struct X x = {1}; // struct X is now in scope

参考

  • C11标准(ISO / IEC 9899:2011):