C Typedef

typedef are used to define a new name of user defined and primitive (inbuilt) data type. This aliasing can used as a data type. Type aliasing is suitable to provide short readable and meaningful name in existing data type.

typedef

Syntax

typedef data_type new_name;

Explanation

iteam overview
typedef keyword
data_type per defined and user defined data type.
new_name alias name
/* 
  Example of typedef
*/
/*Header file*/
#include <stdio.h>
//typedef 
typedef unsigned int positive;
int main(){
    //create variable
  positive num1=10,num2=20;
  //print variable value
  printf("num1 : %d\n",num1 );
  printf("num2 : %d\n",num2 );

}
typedef Example

Output

num1 : 10
num2 : 20

In this program we are change the data type (unsigned int) to user defined name like positive. so conclusion is typedef is capable to change the name of data type. and we are also capable to use original data name. for example.

/* 
  Example of typedef
*/
/*Header file*/
#include <stdio.h>
//typedef 
typedef char symbol;
int main(){
  //typedef symbol
  symbol plus='+', minus='-';
  char divide='/';//normal char variable
  printf("operator : %c %c %c",plus,minus,divide);
}
typedef Example

Output

operator : + - /

There are possible to use typedef in multiple time in single program. for example

/* 
  Example of typedef
*/
/*Header file*/
#include <stdio.h>
//typedef char
typedef char symbol;
int main(){
  //typedef symbol to operator
  typedef symbol oprator;

  oprator plus='+', minus='-';
    symbol dollar='$',at='@';

    printf("oprator : %c %c\n",plus,minus);
    printf("symbol : %c %c\n",dollar,at );

}

Output

oprator : + -
symbole : $ @

typedef are use in user defined data type. for example

/* 
  Example of typedef
*/
/*Header file*/
#include <stdio.h>
typedef struct Student{
  char name[30];
  char roll_number[12];
  int enrol_id;
  int section;

}student;
int main(){
  student s1,s2;
  //code here
}


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved