C Reference ยท Keywords and Statements
The typedef keyword
Reference for the C typedef declaration: create aliases for structs, enums, arrays and function pointers, and learn what typedef does not do.
typedef gives a new name to an existing type. It does not create a new type: the alias and the original are completely interchangeable, and the compiler treats them as the same type. The syntax looks like an ordinary declaration with typedef in front, and the name being declared becomes the alias instead of a variable.
Programmers use it to shorten struct declarations, to hide awkward declarators such as function pointers, and to give meaningful names to integer types (size_t, uint32_t). Because aliases can be swapped in one place, they also help when a type must change later.
typedef unsigned long ulong_t; typedef struct point { int x, y; } point;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
typedef existing-type alias; typedef existing-type alias1, *alias2, alias3[4];
| Part | Description |
|---|---|
existing-type |
Any type, including an unnamed struct, union or enum defined right there. |
alias |
The new name. It is a declarator, so pointer, array and function syntax apply to it. |
Returns: nothing; typedef is a declaration, not an expression.
Errors: redefining an alias to a different type in the same scope is a constraint violation. Since C11, repeating a typedef to the same type is allowed.
Usage notes
- Read it like a declaration. Remove the word
typedefand the declared identifier is the variable you would have gotten; withtypedefit is the alias. - No new type. Aliases are not type-safe wrappers. Passing a
MeterswhereFeetis expected (bothdouble) compiles without warning. - Struct tags and aliases live in different name spaces.
typedef struct node node;lets you writestruct nodeandnodeinterchangeably, and is required for self-referencing types: use the tag inside the struct (struct node *next;). - Pointer aliases can hide
const. Withtypedef char *String;,const String smeanschar *const s(a constant pointer), notconst char *s. Avoid hiding pointers behind aliases. - Opaque types. A header can declare
typedef struct Stack Stack;and keep the definition private to the.cfile. - Scope rules apply. A typedef inside a function is visible only in that block.
- Naming conventions. The POSIX standard reserves names ending in
_t; many projects use CamelCase or their own suffix to stay clear. - With
typeof:typedef typeof(expr) T;names the type of any expression (C23).
Related
struct,union,enum,typeof, function pointers- Lessons: typedef, Function pointers