Typedef Specifier
The typedef
declaration introduces a name that, within its scope, becomes a synonym for the specified type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by the language or declared by the user.
Typedef names allow you to encapsulate implementation details that may change. Unlike the struct
, union
, and enum
declarations, the typedef
declarations do not introduce new types, but new names for existing types.
The specifier typedef
stands first in the declaration:
typedef <type_definition> synonym;
The typedef
keyword assigns synonym
to <type_definition>
. The synonym
needs to be a valid identifier.
A declaration starting with the typedef
specifier does not introduce an object or a function of a given type, but rather a new name for a given type. In other words, the typedef
declaration is identical to a “normal” declaration, but instead of objects, it declares types. It is a common practice to name custom type identifiers with starting capital letter — this is not required by the mikroC PRO for ARM.
For example:
/* Let's declare a synonym for "unsigned long int" */ typedef unsigned long int Distance; /* Now, synonym "Distance" can be used as type identifier: */ Distance i; // declare variable i of unsigned long int
In the typedef
declaration, as in any other declaration, several types can be declared at once. For example:
typedef int *Pti, Array[10];
Here, Pti
is a synonym for type “pointer to int
”, and Array
is a synonym for type “array of 10 int
elements”.
What do you think about this topic ? Send us feedback!