C Tutorial C References

C - Typedef



typedef is a keyword in C language which is used to assign alternative name to existing datatype. It does not introduce a distinct type, it only establishes a synonym for an existing datatype. The syntax for typedef declaration is given below:

Syntax

typedef <existing_name> <alias_name>  

Where, existing_name is the name of an already existing data type and alias_name is the alternative name given to it. For example, To give a name MyInt to unsigned int, the following declaration can be used:

typedef unsigned int MyInt; 

Example:

In the example below, unsigned int is typedef declared as MyInt, which is further used in a for loop to print it.

#include <stdio.h>
 
int main (){
  typedef unsigned int MyInt; 

  for(MyInt i = 1; i <= 5; i++){
    printf("%u\n", i);
  }
  return 0;
}

The output of the above code will be:

1
2
3
4
5

Using typedef with structures

The typedef declaration can be used to give a name to user-defined data types as well. For example, typedef can be used with structures to define a new data type and then use that data type to define structure variables.

Example:

In the example below, a variable emp of type struct Employee is created, which is further used to store data of an Employee.

#include <stdio.h>
#include <string.h>
 
typedef struct Employees {
   char name[50];
   int age;
   char city[50];
   int salary;
} Employee;

int main (){
  Employee emp;

  strcpy(emp.name, "John");
  emp.age = 25;
  strcpy(emp.city, "London");
  emp.salary = 75000;
 
  printf("Employee name: %s\n", emp.name);
  printf("Employee age: %d\n", emp.age);
  printf("Employee city: %s\n", emp.city);
  printf("Employee salary: %d\n", emp.salary);

  return 0;
}

The output of the above code will be:

Employee name: John
Employee age: 25
Employee city: London
Employee salary: 75000

Using typedef with pointers

Similarly, an another name or alias name can be provided to pointer variables by using typedef declaration.

Example:

In the example below, integer pointer type is given an another name intptr.

#include <stdio.h>

int main (){
  //typedef declaration
  typedef int* intptr;

  int MyVar = 10;
  //using intptr instead of int*
  intptr p1;
  
  p1 = &MyVar;

  printf("Address stored in p1 variable: %p\n",p1); 
  printf("Value stored in *p1: %i",*p1);
  return 0;
}

The output of the above code will be:

Address stored in p1 variable: 0x7ffe2c4a86cc
Value stored in *p1: 10

typedef vs #define

#define is a C-directive which is also used to define the aliases for various data types similar to typedef, but with the following differences:

  • typedef is limited to giving symbolic names to types only, whereas #define can be used to define an alias for values as well, for example: 3.14 can be defined as PI.
  • typedef interpretation is performed by the compiler where #define statements are performed by pre-processor.
  • #define is not terminated with a semicolon, but typedef is terminated with a semicolon.
  • typedef follows the scope rule i.e., if a new type is defined in a scope (inside a function or block), then the new type name will only be visible within the scope. Whereas in case of #define, when the pre-processor encounters #define, it replaces all the occurrences.

Example:

The example below shows the usage of #define C-directive.

#include <stdio.h>

#define PI 3.14 
#define MyInt int
#define False 0

int main (){
  MyInt x = 10;

  printf("x = %i\n", x); 
  printf("PI = %f\n", PI);
  printf("False = %i\n", False);
  return 0;
}

The output of the above code will be:

x = 10
PI = 3.140000
False = 0

5