The typedef
keyword in C allows you to defined new types.
C语言中的typedef
关键字允许您定义新类型。
Starting from the built-in C types, we can create our own types, using this syntax:
从内置的C类型开始 ,我们可以使用以下语法创建自己的类型:
typedef existingtype NEWTYPE
The new type we create is usually, by convention, uppercase.
按照惯例,我们创建的新类型通常是大写的。
This it to distinguish it more easily, and immediately recognize it as type.
这样可以更容易地区分它,并立即将其识别为类型。
For example we can define a new NUMBER
type that is an int
:
例如,我们可以定义一个新的NUMBER
类型为int
:
typedef int NUMBER
and once you do so, you can define new NUMBER
variables:
然后,您可以定义新的NUMBER
变量:
NUMBER one = 1;
Now you might ask: why? Why not just use the built-in type int
instead?
现在您可能会问:为什么? 为什么不只使用内置类型int
呢?
Well, typedef
gets really useful when paired with two things: enumerated types and structures.
好吧,当与两个东西配对时, typedef
会变得非常有用:枚举类型和结构。