The C programming language provides a keyword called typedef to set an alternate name to an existing data type. The typedef keyword in C is very useful in assigning a convenient alias to a built-in data type as well as any derived data type such as a struct, a union or a pointer.
Sometimes it becomes clumsy to use a data type with a longer name (such as "struct structname" or "unsigned int") every time a variable is declared. In such cases, we can assign a handy shortcut to make the code more readable.
In general, the typedef keyword is used as follows −
typedef existing_type new_type;
In this chapter, let us have a look at some of the use-cases of typedef.
In C language, the keyword "unsigned" is used to declare unsigned integer variables that can store only non-negative values.
C also has a keyword called "short" that declares an integer data type that occupies 2 bytes of memory. If you want to declare a variable that is short and can have only non-negative values, then you can combine both these keywords (unsigned and short):
short unsigned int x;
If there are going to be many variables to be declared of this type, it will not be very convenient to use these three keywords every time. Instead, you can define an alias or a shortcut with the typedef keyword as follows −
typedef short unsigned int USHORT;
This tells the compiler that the identifier USHORT corresponds to "short unsigned int" type. Hereafter, you can use USHORT in the variable declaration statement −
USHORT x;
C also has the keyword static to indicate that such a variable is initialized only once. The keyword "long" allocates 8 bytes to store an integer on a 64-bit system. We can declare a variable of this type as follows −
static unsigned long x;
However, we can’t use the keyword "static" in a "typedef" statement, however we can use typedef to assign a shortcut alias to this type of declaration −
typedef signed long SLONG; static SLONG x;
Note: As a convention, the alias names are expressed in uppercase, just to differentiate between the built-in type and the aliases used.
The following example demonstrates how you can use alias names in a C program −
#include int main()When you run this code, it will produce the following ouput −
Size of SLONG: 8 Size of USHORT: 2
Normally, we need to declare a struct variable by prefixing the name of struct_type in the declaration statement as −
struct struct_type var;
If writing the type name in this manner feels cumbersome, then you can use typedef to assign an alias −
typedef struct struct_type ALIAS;
In this example, we define a structure type and then use the typedef keyword to set an alias for it −
#include int main() < typedef unsigned long int ULONG; typedef short int SHORT; struct mystruct < ULONG a; SHORT b; >; typedef struct mystruct STR; STR s1 = ; printf("%ld %u", s1.a, s1.b); return 0; >
Run the code and check its output −
10 20
There is an alternate approach to use the typedef keyword. We can combine it in the structure definition itself, as given below −
typedef struct mystruct < ULONG a; SHORT b; >STR; STR s1 = ;
The typedef keyword may also be used to assign a new identifier to any pointer type. Normally, we declare a pointer variable as follows −
struct mystruct * x;
Instead, we can use the typedef keyword as follows −
typedef struct mystruct < ULONG a; SHORT b; >STR; typedef STR * strptr;
It allows you to declare a pointer of this type in a much more concise way −
strptr ptr;
We can then assign the address of a corresponding struct variable to the pointer.
The following example shows how you can use typedef to create a struct prointer −
#include int main() < typedef unsigned long int ULONG; typedef short int SHORT; typedef struct mystruct < ULONG a; SHORT b; >STR; STR s1 = ; typedef STR * strptr; strptr ptr = &s1; printf("%d %d \n", s1.a, s1.b); printf("%d %d", ptr->a, ptr->b); return 0; >
When you run this code, it will produce the following output −
10 20 10 20
We can use the typedef keyword to assign a shortcut alias to any union type.
The following example illustrates how you can use typedef in creating unions −
#include int main() < typedef unsigned long int ULONG; typedef short int SHORT; typedef union myunion < char a; int b; double c; >UNTYPE; UNTYPE u1; u1.c = 65.50; typedef UNTYPE * UNPTR; UNPTR ptr = &u1; printf("a:%c b: %d c: %lf \n", u1.a, u1.b, u1.c); printf("a:%c b: %d c: %lf \n", ptr->a, ptr->b, ptr->c); return 0; >
Run the code and check its output −
a: b: 0 c: 65.500000 a: b: 0 c: 65.500000
The typedef keyword is often confused with the #define directive. In C language, #define is a preprocessor directive. It is an effective method to define a constant.
The syntax of using #define is as follows −
#define name value
For example −
#define PI 3.14159265359
The #define statement can also be used to define a macro −
#define SQUARE(x) x*x
A macro works like a function. However, the value is substituted at the preprocessor level when called.
printf("%d", SQUARE(5));
#define is a preprocessor directive, while typedef is evaluated at the time of compilation.
In the following code, we use both these features (typedef and #define) −
#include #define MAX 10 int main() < typedef unsigned long int ULONG; typedef short int SHORT; typedef struct employee < char name[MAX]; int age; >EMP; EMP e1 = ; printf("Name: %s \nAge: %d", e1.name, e1.age); return 0; >
When you run this code, it will produce the following output −
Name: Krishna Age: 25