typedef in C Programming Language
Notes:
typedef statement in c OR type definition statement in C :
- typedef statement allows us to define an alternative name to any data type
- typedef statement is mainly used to minimize typing data type names
Syntax:
typedef currentDataTypeName alternativeName;
Ex:
typedef unsigned int uint;
Note: Once we define an alternative name to any data type; variables, constants, arrays etc. can be declared with the new alternative name;
Ex:
typedef unsigned int uint;
uint num1 = 10;
uint num2 = 20;
uint sum = num1 + num2;
printf("%u\n",sum);
Note: typedef statement is most commonly used to give alternative names to user defined data types, i.e. typedef statement is most commonly used with structures and unions
Ex:
struct Date
{
int day;
int month;
int year;
};
typedef struct Date Date;
Date date1 = {1,1,2020};
Date date2 = {2,2,2020};
printf("date1 = %d/%d/%d\n",date1.day,date1.month,date1.year);
printf("date2 = %d/%d/%d\n",date2.day,date2.month,date2.year);