C Typedef

typedef

typedef is a keyword which are used to defined a new name in existing or user defined data type. if we are append typedef in front of data type then compiler are understand this data type are new alias name.

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
}

© 2019, regularcodes.com, All rights reserved