C Programming Tutorial


Typedef is a keyword used in C programming to give some meaningful name to a variable that already exists in a C language program. It behaves the same way when we define an alias for a command. In short, we can say that this keyword is used to redefine an existing variable name.


Syntax of typedef

					typedef <existing_name> <alias_name>  

In the above syntax, “existing_name” is the name of a pre-existing variable, while “alias name” is another name given to the existing variable.

For example, if we need to claim a few variables of kind unsigned int, we ought to write unsigned int in software, which may be pretty disturbing for a number of us. We can then assign a brand new call of our preference to unsigned int with typedef, which may be used at any time if we need to apply unsigned int in software.

					typedef unsigned int uint;  
unit p, q ;

Now, we can write units withinside the complete application in preference to unsigned int. The above code is similar to writing:

					unsigned int p, q;

Let’s see an example.

					#include <stdio.h>
int main()
    typedef unsigned int ui;
    ui p = 9, q = 8;
    printf("p = %d\n", p);
    printf("q = %d\n", q);
    return 0;
p = 9
q = 8

Then we can assign a new name to each data type.

Application of typedef

Typedef keyword is used in lots of locations in C programming. But primarily, it makes use of user-defined data types kinds like structure, union or enum. So here, we’re going to speak about a few locations in which you could use typedef accurately, which will increase the code clarity and complexity.
Let’s see its use with Structures and Pointers.

Using typedef with struct

Consider the following structure declaration:

					struct student  
    char name[15];  
    int age;  
struct student a1;  

In the structure declaration above, we create the student type variable by writing the following declaration:

					struct student a1;  

The above statement suggests the advent of a variable, i.e., a1. However, the statement is pretty massive. To keep away from any such huge statement, we use the typedef keyword to create the variable of the type student.

From the above statements, we conclude that the typedef keyword reduces the length of the code and the complexity of the data types. It also helps to understand the c program.

Similarly, we can use typedef to assign a new name to a structure that is a user-defined data type, as shown below.

					typedef struct student _ name
    data-type student-1;
    data-type student-2;
    data-type student-3;
    data-type student-4;

Now, whilst putting forward variables of this structure type, we will write type_name in the location of struct structure_name withinside the complete program.

Using typedef with pointers

We have many cases wherein the use of typedef is useful at some point of pointer declaration.

In Pointers * binds to the right and is now no longer at the left.

We can also give the pointer variables a different name or alias using typedef.

For example, we usually declare pointers, as shown below:

					int* ptr;

We can rename the above pointer variable as follows:

					typedef int * ptr;  

In the previous statement, we declared a variable of type int*. Now, we can create a variable of type int* simply by using the variable ‘ptr’ as shown in the following statement:

					ptr q1, q2 ;  

Advantages of Typedef in C

The typedef keyword lets the programmer create new names for sorts inclusive of int or, extra usually in C++, templated sorts–it actually stands for “type definition”. Typedefs may be used each to offer extra readability to your code and to make it less complicated to make modifications to the underlying data types that you use.

Typedefs assist you in providing greater unique names for integrated types: for instance, you may frequently see the size_t kind used in preference to a simple antique unsigned int, whilst it is beneficial to make clean that the value is predicted for use as a size. For example, the strlen function returns a “size_t” kind in preference to an unsigned int.

Using the typedef, we will do away with the greater keystroke; for instance, in structure, if you’ll use the typedef, you do now no longer require to write down struct keyword on the time of variable declaration. In return, it will increase the portability of the code.