非常教程

C参考手册

C 语法

Typedef declaration

typedef声明提供了一种方式来声明的标识符作为一个类型别名,要使用的替换可能复杂的类型名称。

该关键字typedef在声明中用于存储类说明符的语法位置,但不影响存储或关联:

typedef int int_t; // declares int_t to be an alias for the type int
typedef char char_t, *char_p, (*fp)(void); // declares char_t to be an alias for char
                                           // char_p to be an alias for char*
                                           // fp to be an alias for char(*)(void)

说明

如果声明使用typedef存储类说明符,则其中的每个声明符都将标识符定义为指定类型的别名。由于声明中只允许使用一个存储类说明符,因此typedef声明不能为静态或外部。

typedef声明不会引入独特的类型,它只会为现有类型建立同义词,因此typedef名称与它们所属的类型兼容。Typedef名称与普通标识符(如枚举器,变量和函数)共享名称空间。

VLA的typedef只能出现在块范围内。每次控制流经过typedef声明时,都会评估数组的长度,而不是声明数组本身:void copyt(int n){typedef int Bn; // B是VLA,其大小为n,现在评估n + = 1; B a; // a的大小是从之前的+ n = 1 int bn; // a和b的大小不同(int i = 1; i <n; i ++)ai-1 = bi; }

(自C99以来)

注释

typedef名称可能是不完整的类型,可以像往常一样完成:

typedef int A[]; // A is int[]
A a = {1, 2}, b = {3,4,5}; // type of a is int[2], type of b is int[3]

通常使用typedef声明将名称从标记名称空间注入到普通名称空间中:

typedef struct tnode tnode; // tnode in ordinary name space
                            // is an alias to tnode in tag name space
struct tnode {
    int count;
    tnode *left, *right; // same as struct tnode *left, *right;
}; // now tnode is also a complete type
tnode s, *sp; // same as struct tnode s, *sp;

他们甚至可以避免使用标签名称空间:

typedef struct { double hi, lo; } range;
range z, *zp;

Typedef名称通常也用于简化复杂声明的语法:

// array of 5 pointers to functions returning pointers to arrays of 3 ints
int (*(*callbacks[5])(void))[3]
 
// same with typedefs
typedef int arr_t[3]; // arr_t is array of 3 int
typedef arr_t* (*fp)(void); // pointer to function returning arr_t*
fp callbacks[5];

库经常将依赖于系统或依赖于配置的类型显示为typedef名称,以向用户或其他库组件提供一致的接口:

#if defined(_LP64)
typedef int     wchar_t;
#else
typedef long    wchar_t;
#endif

参考

  • C11标准(ISO / IEC 9899:2011):
    • 6.7.8类型定义(p:137-138)
  • C99标准(ISO / IEC 9899:1999):
    • 6.7.7类型定义(p:123-124)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 3.5.6类型定义

关键词

typedef.

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.Type
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 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。