typedef in c

Previous

typedef:

  • It is a keyword.
  • It is user defined data type.
  • It is used to create synonyms to existing data types.
  • Typedef declarations are not permanent.
  • Once the program execution completes, the synonyms will be deleted.
  • Typedef declaration will not occupy memory.
#include<stdio.h>
int main()
{
	int a ;
	printf("Enter a value is : ");
	scanf("%d", &a);
	printf("Input value is : %d \n", a);
	return 0;
}

In the above code, we use primitive data type int. We can create duplicate name to int data type using typedef declaration.

#include<stdio.h>
int main()
{
	typedef int Integer;
	Integer a ;
	
	printf("Enter a value is : ");
	scanf("%d", &a);
	printf("Input value is : %d \n", a);
	return 0;
}

Addition program using typedef declaration:

#include<stdio.h>
int main()
{
	typedef int Integer;
	Integer a=10, b=20, c ;
	c = a+b;
	printf("Sum is : %d \n", c);
	return 0;
}
  • We can create typedef declarations to derived data types such as arrays, strings and pointers.
  • Array type declaration as follows.
#include<stdio.h>
int main()
{
	int arr[5] = {10,20,30,40,50};
	int i;
	
	printf("Array elements are : \n");
	for(i=0 ; i<5 ; i++)
	{
		printf("%d \n", arr[i]);
	}
	return 0;
}

In the above code, we can declare array variable using pre-defined typedef array:

#include<stdio.h>
int main()
{
	typedef int Array[5]; 
	Array x = {10,20,30,40,50};
	int i;
	
	printf("Array elements are : \n");
	for(i=0 ; i<5 ; i++)
	{
		printf("%d \n", x[i]);
	}
	return 0;
}
  • Complex pointers we can easily replace with simple typedef declarations.
  • Char* can points to single character or a set of characters.
  • We might get confuse whether the pointer is to a single character or to a string.
#include<stdio.h>
char* read(void);
int main()
{
	char* name;
	name = read();
	printf("Input name is : %s \n", name);
	return 0;
}
char* read(void)
{	
	char* name;
	printf("Enter your name : ");
	gets(name);
	return name;
}
  • In the above program, we confuse about the type of variable name.
  • Char* points to a String here.
  • Char* can points to single character also.
  • In C language, we don’t have String type to distinguish correctly
  • We can create String type using typedef declaration.
#include<stdio.h>
typedef char* String;
String read(void);
int main()
{
	String name;
	name = read();
	printf("Input name is : %s \n", name);
	return 0;
}
String read(void)
{	
	String name;
	printf("Enter your name : ");
	gets(name);
	return name;
}
  • Using typedef declarations, we can easily specify structure types.
  • Generally struct type variable need to represent with 2 words (struct name var)
#include<stdio.h>
struct Emp
{
	int id;
	char name[20];
};
int main()
{
	struct Emp e;
	printf("Enter Emp id and name : ");
	scanf("%d%s", &e.id, e.name);
	
	printf("Emp name is : %s \n", e.name);
	return 0;
}

When we apply typedef declaration to Struct type, the program become more readable and understandable as follows.

#include<stdio.h>
struct Emp
{
	int id;
	char name[20];
};
int main()
{
	typedef struct Emp Employee;
	Employee e;
	printf("Enter Emp id and name : ");
	scanf("%d%s", &e.id, e.name);
	
	printf("Emp name is : %s \n", e.name);
	return 0;
}

We can specify the typedef declaration directly in the definition of structure type:

#include<stdio.h>
typedef struct Emp
{
	int id;
	char name[20];
}Employee;
int main()
{
	Employee e;
	printf("Enter Emp id and name : ");
	scanf("%d%s", &e.id, e.name);
	
	printf("Emp name is : %s \n", e.name);
	return 0;
}

Using typedef declarations, we can reduce the size of expressions

#include<stdio.h>
struct emp
{
	int eno;
	char ename[20];
	float esal;
};
typedef struct emp e; 
typedef struct emp* ptr;
void main()
{
	ptr p;
	p=(ptr)malloc(sizeof(e));	
}
Previous

Add Comment

Courses Enquiry Form