C Typedef is nothing but "Type-Definition".
C Typedef is used to give alternative names to the original names to reduce the complexity of the program. They acts as "alias names" or "shorthand versions" of structure names/data-types.
Syntax
typedef original_name alternate_name;
Eg:
typedef int number;
number x=10; //number really means int
Example
[c]#include<stdio.h>
#include<string.h>
#include<limits.h>
int main()
{
typedef long long int lli;
printf("Storage size for lli type: %ld", sizeof(lli));
return 0;
}[/c]
Output:
[c]Storage size for lli type: 8[/c]
Advantages
Checking for bugs becomes easier.
type-def reduces the memory size also.
User finds no complexity in writing the program.
Simplification of the commands can be done as per the need.
It deals with platform independent issues.
Typedef in functions
Description
To declare a name to which already C typedef exists, that name should consists of a type-specifier to avoid confusion. If want to use typedef with function, specify only the return value not all the parameters of the function.
Example
[c]
#include <stdio.h>
#include <string.h>
typedef struct student
{
int id;
char name[20];
float percentage;
} status;
int main()
{
status record;
record.id=1;
strcpy(record.name, "James");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}[/c]
Output:
[c]
Id is: 1
Name is: James
Percentage is: 86.500000
[/c]
Summary
Key Points
C Typedef gives user-defined data types.
All data types defined will be under the control of compiler.
Programming
Tips
Declarations must be clear to avoid confusion between two alias names.