C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C Typedef

C Typedef

shape Description

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.

shape Syntax

typedef original_name  alternate_name;
Eg: typedef int number; number x=10; //number really means int

shape 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]

shape Advantages

Typedef in functions

shape 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.

shape 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

shape Key Points

  • C Typedef gives user-defined data types.
  • All data types defined will be under the control of compiler.

shape Programming Tips

Declarations must be clear to avoid confusion between two alias names.