The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

typedef in C

In C programming language, typedef is a keyword used to create alias name for the existing datatypes. Using typedef keyword we can create a temporary name to the system defined datatypes like int, float, char and double. we use that temporary name to create a variable. The general syntax of typedef is as follows...

typedef    <existing-datatype>    <alias-name>

typedef with primitive datatypes

Consider the following example of typedef

typedef   int   Number

In the above example, Number is defined as alias name for integer datatype. So, we can use Number to declare integer variables.

Example Program to illustrate typedef in C.

#include<stdio.h>
#include<conio.h>

typedef int Number;

void main(){

   Number a,b,c;    // Here a,b,&c are integer type of variables.
   
   clrscr() ;
   printf("Enter any two integer numbers: ") ;
   scanf("%d%d", &a,&b) ;
   
   c = a + b;
   
   printf("Sum = %d", c) ;
}

typedef with Arrays

In C programming language, typedef is also used with arrays. Consider the following example program to understand how typedef is used with arrays.

Example Program to illustrate typedef with arrays in C.

#include<stdio.h>
#include<conio.h>

void main(){

   typedef int Array[5];    // Here Array acts like an integer array type of size 5.
   
   Array list = {10,20,30,40,50};   // List is an array of integer type with size 5.
   int i;
   
   clrscr() ;

   printf("List elements are : \n") ;
   
   for(i=0; i<5; i++)
        printf("%d\t", list[i]) ;
}

In the above example program, Array is the alias name of integer array type of size 5. We can use Array as datatype to create integer array of size 5. Here, list is an integer array of size 5.


typedef with user defined datatypes like structures, unions etc.,

In C programming language, typedef is also used with structures and unions. Consider the following example program to understand how typedef is used with structures.

Example Program to illustrate typedef with arrays in C.

#include<stdio.h>
#include<conio.h>

typedef struct student
{
    char stud_name[50];
    int stud_rollNo;
}stud;

void main(){

   stud s1;
   clrscr() ;

   printf("Enter the student name: ") ;
   scanf("%s", s1.stud_name);
   printf("Enter the student Roll Number: ");
   scanf("%d", &s1.stud_rollNo);
   
   printf("\nStudent Information\n");
   printf("Name - %s\nHallticket Number - %d", s1.stud_name, s1.stud_rollNo);
}

In the above example program, stud is the alias name of student structure. We can use stud as datatype to create variables of student structure. Here, s1 is a variable of student structure datatype.


typedef with Pointers

In C programming language, typedef is also used with pointers. Consider the following example program to understand how typedef is used with pointers.

Example Program to illustrate typedef with Pointers in C.

#include<stdio.h>
#include<conio.h>


void main(){

   typedef int* intPointer;
   
   intPointer ptr;  // Here ptr is a pointer of integer datatype.
   int a = 10;
   
   clrscr() ;
   
   ptr = &a;
   
   printf("Address of a = %u ",ptr) ;
   printf("\nValue of a = %d ",*ptr);
}

Place your ad here
Place your ad here