Breaking News
yeni film izle hd film seyret film izle

typedef In C Programming Language

typedef is a keyword used in C language to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated. Following is the general syntax for using typedef,

typedef existing_name alias_name

Lets take an example and see how typedef actually works.

typedef unsigned long ulong;

The above statement define a term ulong for an unsigned long type. Now this ulong identifier can be used to define unsigned long type variables.

ulong i, j ;

Application of typedef

typedef can be used to give a name to user defined data type as well. Lets see its use with structures.

typedef struct
{
  type member1;
  type member2;
  type member3;
} type_name ;

Here type_name represents the stucture definition associated with it. Now this type_name can be used to declare a variable of this stucture type.

type_name t1, t2 ;

Example of structure definition using typedef

#include<stdio.h>
#include<conio.h>
#include<string.h>
 
typedef struct employee
{
 char  name[50];
 int   salary;
} emp ;
 
void main( )
{
 emp e1;
 printf("\nEnter Employee record\n");
 printf("\nEmployee name\t");
 scanf("%s",e1.name);
 printf("\nEnter Employee salary \t");
 scanf("%d",&e1.salary);
 printf("\nstudent name is %s",e1.name);
 printf("\nroll is %d",e1.salary);
 getch();
}

typedef and Pointers

typedef can be used to give an alias name to pointers also. Here we have a case in which use of typedef is beneficial during pointer declaration.

In Pointers * binds to the right and not the left.

int* x, y ;

By this declaration statement, we are actually declaring x as a pointer of type int, whereas y will be declared as a plain integer.

typedef int* IntPtr ;
IntPtr x, y, z;

But if we use typedef like in above example, we can declare any number of pointers in a single statement.

NOTE : If you do not have any prior knowledge of pointers, do study Pointers first.

Check Also

Program to Remove duplicate Element in an Array

Program to Remove duplicate Element in an Array C program to delete duplicate elements from …