Defines a new type based on an existing type. Its syntax mirrors that of a variable declaration.
/* Byte can be used wherever `unsigned char` is needed */
typedef unsigned char Byte;
/* Integer is the type used to declare an array consisting of a single int */
typedef int Integer[1];
/* NodeRef is a type used for pointers to a structure type with the tag "node" */
typedef struct node *NodeRef;
/* SigHandler is the function pointer type that gets passed to the signal function. */
typedef void (*SigHandler)(int);
While not technically a storage class, a compiler will treat it as one since none of the other storage classes are allowed if the typedef
keyword is used.
The typedef
s are important and should not be substituted with #define
macro.
typedef int newType;
newType *ptr; // ptr is pointer to variable of type 'newType' aka int
However,
#define int newType
newType *ptr; // Even though macros are exact replacements to words, this doesn't result to a pointer to variable of type 'newType' aka int