非常教程

C参考手册

C 语法

Type

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

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

类型分类

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

  • 方式 void
  • 基本类型
  • 方式 char
  • 有符号整数类型
    • 标准:signed charshortintlonglong long (因为C99)
    • 扩展:定义的实现,例如 __int128
  • 无符号整数类型
    • 标准:_Bool (自C99), , ,unsigned char,,unsigned short ( 因为C99)unsigned intunsigned longunsigned long long
    • 扩展:实现定义,例如 __uint128
  • 浮动类型
    • 真正的浮点类型:floatdoublelong double
    • 复杂类型:float _Complexdouble _Complexlong double _Complex
    • 虚类型:float _Imaginarydouble _Imaginarylong double _Imaginary
  • 枚举类型
  • 派生类型
    • 数组类型
    • 结构类型
    • 工会类型
    • 函数类型
    • 指针类型
    • 原子类型

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

类型组

  • 对象类型:不是函数类型的所有类型
  • 字符类型charsigned charunsigned char
  • 整数类型char,有符号整数类型,无符号整数类型,枚举类型
  • 实际类型:整数类型和实际浮动类型
  • 算术类型:整数类型和浮点类型
  • 标量类型:算术类型和指针类型
  • 聚合类型:数组类型和结构类型
  • 派生的声明器类型:数组类型,函数类型和指针类型

兼容的类型

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

类型TU兼容。

  • 它们是相同的类型(由typedef引入的同名或别名)
  • 它们是兼​​容的不合格类型的相同cvr合格版本
  • 它们是指针类型,并指向兼容的类型
  • 他们是数组类型,并且
    • 他们的元素类型是兼容的,并且
    • 如果两者都具有恒定的大小,那么大小是相同的。注意:未知边界的数组与兼容元素类型的任何数组兼容。VLA与任何兼容元素类型的阵列兼容。(自C99以来)
  • 它们都是结构/联合/枚举类型,并且
  • (C99)如果用标签声明了一个,另一个也必须用相同的标签声明。
  • 如果两者都是完整类型,则它们的成员必须完全对应数量,使用兼容类型声明并具有匹配的名称。
  • 另外,如果它们是枚举,相应的成员也必须具有相同的值。
  • 另外,如果他们是结构或工会,
    • 相应的成员必须以相同的顺序进行声明(仅限结构)
    • 相应的位域必须具有相同的宽度。
  • 一个是枚举类型,另一个是枚举的基础类型
  • 他们是功能类型,和
    • 他们的返回类型是兼容的
    • 它们都使用参数列表,参数的数量(包括使用省略号)是相同的,相应的参数具有兼容的类型
    • 一个是旧样式(无参数)定义,另一个具有参数列表,参数列表不使用省略号,每个参数在缺省参数后都与相应的旧式参数兼容(在函数参数类型调整后)促销
    • 一个是旧式(无参数)声明,另一个是参数列表,参数列表不使用省略号,所有参数(函数参数类型调整后)不受默认参数促销影响

该类型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
  • sizeof

复合文字

(自C99以来)

通用选择alignof alignas _Atomic(当用作类型说明符时)

(自C11以来)

  • 复合文字

(since C99)

  • generic selection
  • alignof
  • alignas
  • _Atomic(当用作类型说明符时)

(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):
    • 6.2.5类型(p:39-43)
    • 6.2.6类型的表示(p:44-46)
    • 6.2.7兼容型和复合型(p:47-48)
  • C99标准(ISO / IEC 9899:1999):
    • 6.2.5类型(p:33-37)
    • 6.2.6类型的表示(p:37-40)
    • 6.2.7兼容型和复合型(p:40-41)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 3.1.2.5类型
    • 3.1.2.6兼容类型和复合类型

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
59.Logical operators
60.Lookup and name spaces
61.Main function
62.Member access operators
63.Memory model
64.Objects and alignment
65.Order of evaluation
66.Other operators
67.Phases of translation
68.Pointer declaration
69.Preprocessor
70.restrict type qualifier
71.return statement
72.Scalar initialization
73.Scope
74.sizeof operator
75.Statements
76.static assert declaration
77.Static storage duration
78.Storage-class specifiers
79.string literals
80.Struct and union initialization
81.Struct declaration
82.switch statement
83.Thread storage duration
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。