ProAnswers.org

What is the use of typedef?

1 Like

typedef declaration helps to make source code of a C program more readable. Its purpose is to redefine the name of an existing variable type. It provides a short and meaningful way to call a data type. typedef is useful when the name of the data type is long. Use of typedef can reduce length and complexity of data types.
Note: Usually uppercase letters are used to make it clear that we are dealing with our own data type.

[b]Example: [/b]




	
		 
	
		
			
				
					
						1
					
						2
					
						3
					
						4
					
						5
					
						6
				
				
					
						
							struct employee {
						
							 char name[20];
						
							 int age;
						
							};
						
							 
						
							struct employee e;
					
				
			
		
	



The above declaration of the structure would be easy to use when renamed using typedef as:



 


	
		
			
				1
			
				2
			
				3
			
				4
			
				5
			
				6
			
				7
		
		
			
				
					struct employee {
				
					 char name[20];
				
					 int age;
				
					};
				
					 
				
					typedef struct employee EMP;
				
					EMP e1, e2;

The typedef help in easier modification when the programs are ported to another machine.

A descriptive new name given to the existing data type may be easier to understand the code.