typedef is used to create new name for existing data type. Below is the general representation of typedef statement.
typedef <old_data_name> <new_data_type>;
Consider the following,
typedef int integer;
typedef char character;
Here, integer is the new name for the data type int and character is the new name for the data type char. So, any integer or character variable can be declared as follows.
integer a, b;
character ch;
typedef can also be used on compound data type. Consider the following
typedef struct student{
char name[100];
int age;
}learner;
Here, learner is the new name for the data type struct student. Then, the variable for struct student can be declared as follows.
typedef struct student{
char name[100];
int age;
}learner;
learner s1, s2;
Let us see how to perform type definition for arrays. Consider the following,
typdef int integer_array[5];
integer_array arr = {1, 2, 3, 4, 5};
Here, arr is an array of 5 integers.
typedef <old_data_name> <new_data_type>;
Consider the following,
typedef int integer;
typedef char character;
Here, integer is the new name for the data type int and character is the new name for the data type char. So, any integer or character variable can be declared as follows.
integer a, b;
character ch;
typedef can also be used on compound data type. Consider the following
typedef struct student{
char name[100];
int age;
}learner;
Here, learner is the new name for the data type struct student. Then, the variable for struct student can be declared as follows.
typedef struct student{
char name[100];
int age;
}learner;
learner s1, s2;
Let us see how to perform type definition for arrays. Consider the following,
typdef int integer_array[5];
integer_array arr = {1, 2, 3, 4, 5};
Here, arr is an array of 5 integers.
Example C program to illustrate typedef in C:
#include <stdio.h> typedef struct student { char name[32]; int age; }learner; // typedef for compound data type typedef int integer; // typedef for integer typedef char character; // typedef for character typedef int int_arr[5]; // typedef for integer array int main() { /* variable initialization */ learner obj1 = {"Tom", 10}; int_arr arr = {1, 2, 3, 4, 5}; integer i, a = 10, b = 20; character ch = 'c'; /* printing the outputs */ printf("obj1.name: %s\n", obj1.name); printf("obj1.age : %d\n", obj1.age); for (i = 0; i < 5; i++) { printf("Value of arr[%d]: %d\n", i, arr[i]); } printf("Sum of %d and %d is %d\n", a, b, a + b); printf("Value of character ch is %c\n", ch); return 0; }
Output:
jp@jp-VirtualBox:~/$ ./a.out
obj1.name: Tom
obj1.age : 10
Value of arr[0]: 1
Value of arr[1]: 2
Value of arr[2]: 3
Value of arr[3]: 4
Value of arr[4]: 5
Sum of 10 and 20 is 30
Value of character ch is c
obj1.name: Tom
obj1.age : 10
Value of arr[0]: 1
Value of arr[1]: 2
Value of arr[2]: 3
Value of arr[3]: 4
Value of arr[4]: 5
Sum of 10 and 20 is 30
Value of character ch is c
Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324
ReplyDelete