English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Language typedef Keyword

C language provides the typedef keyword, which you can use to give a new meaningful name to a type. It is also used to redefine the name of an existing variable.

The syntax of

typedef <existing_name> <alias_name>

In the above syntax, " existing_name"is the name of the existing variable, while "alias_name"is another name given to an existing variable.

For example, suppose we want to create a type ofunsigned intIf we have multiple variables of this type, it can become cumbersome. To overcome this problem, we usetypedefthe keyword.

typedef unsigned int unit;

In the above statement, we usetypedefThe keyword declares an unsigned int typeunitvariable.

Now, we can create a variable by writing the following statementunsigned inttype variables were created:

unit a, b;

Instead of writing:

unsigned int a, b;

You should have already observedtypedefKeywords provide a simplified shortcut by offering an alternative name for existing variables. This keyword is very useful when dealing with long data types, especially structure declarations.

Let's understand through a simple example.

#include <stdio.h>
int main()
{
    typedef unsigned int unit;
    unit i,j;
    i=10;
    j=20;
    printf("i value is :%d",i);
    printf("\nj value is :%d",j);
    return 0;
}

Output result

i value is :10 
j value is :20

typedef for structures

The following is the structure declaration:

struct student
{
char name[20];
int age;
};
struct student s1;

In the above structure declaration, we createdStudenttype variables were created:

struct student s1;

The above statement shows that the variable s1types, but the statement is quite long. To avoid such a long declaration, we usetypedefkeyword to createstudentvariables.

struct student
{
char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;

In the above declaration, we declare a variable of typestud. Now, we can usestudvariables to createstruct studentVariable of a type.

The above typedef can be written as:

typedef struct student
{
    char name[20];
    int age; 
}
stud s1,s2;

From the above declaration, we conclude that,typedefKeywords reduce the length of the code and the complexity of data types. They also help understand the program.

Let's look at another example where typedef structure declaration is used.

#include <stdio.h>
typedef struct student
{
    char name[20];
    int age;
}stud;
int main()
{
    stud s1;
    printf("Enter student s1Detailed information: ");
    printf("\nEnter student name:");
    scanf("%s", &s1.name);
    printf("\nEnter student age:");
    scanf("%d", &s1.age);
    printf("\n Student name : %s", s1.name);
    printf("\n Enter student age : %d", s1.age);
    return 0;
}

Output result

Enter student s1Detailed information: 
Enter student name: Peter
Enter student age: 28 
Student name : Peter 
Student age : 28

typedef in conjunction with pointers

We can also usetypedefProvide another name or alias for the pointer variable.

For example, we usually declare a pointer as follows:

int* ptr;

We can rename the above pointer variables as follows:

typedef int* ptr;

In the above statement, we declaredint *Type variable. Now, we just need to use' ptr'Variable to createint *Variable of a type, as shown in the following statement:

ptr p1, p2 ;

In the above statement,p1andp2is' ptr'Variable of a type.