C

In C, Typedef – The Most Detailed

In the C programming language, the typedef is a reserved word (keyword). It is used to generate a new name for an existing data type. It does not create a new data type, but rather assigns a simple alias to an existing data type so that it can be easily referenced in the program. This guide will teach you how to define and use typedef in C.

Typedef syntax

typedef existing_data_type, alias

For example:

typedef unsigned int pnum

In this case, an unsigned int alias named “pnum” is created. Once you’ve created this alias, you can use it as follows:
Unsigned int variables can be declared as follows:

pnum num1, num2;

Rather than declaring the variable as follows:

unsigned int num1, num2;

To better understand this concept, consider the following example:

#include <stdio.h>
int main()
{
  //creating an alias for unsigned int
  typedef unsigned int qty;

  //this is same as: unsigned int num, num2, num3;
  qty num,num2, num3;
  num=10;
  num2 = 100;
  num3 = 16;
  printf("Value of num: %d",num);
  printf("\nValue of num2: %d",num2);
  printf("\nValue of num3: %d",num3);
  return 0;
}

Output:

Value of num: 10
Value of num2: 100
Value of num3: 16

The use of typedef in structures

When working with structures, typedef is most commonly used. This is due to the fact that structures frequently have long statements that can be made shorter and easier to read by using typedef. Let’s look at how we can use typedef to make structures more user friendly.

struct employee
{
  int id;
  char name[30];
  int contact;
  char address[100];
};

The preceding declaration resulted in the creation of a structure employee. If you want to create a structure variable, do the following:

struct employee e1, e2;

We can simplify and make this process easier to read by using typedef.

struct employee
{
  int id;
  char name[30];
  int contact;
  char address[100];
};
typedef struct employee emp;
emp e1, e2;

Emp now represents the struct employee, and you can declare structure variables with “emp.”
The preceding declaration can be written more simply as follows:

typedef struct employee
{
  int id;
  char name[30];
  int contact;
  char address[100];
}emp;
emp e1, e2;

Let’s see the complete example:

#include <stdio.h>
typedef struct employee
{
  int id;
  char name[30];
  int contact;
  char address[100];
}emp;

int main()
{
  emp e1;
  printf("Enter data for employee");
  printf("\nEnter id: ");
  scanf("%d",&e1.id);
  printf("Enter name: ");
  scanf("%s",&e1.name);
  printf("Enter contact: ");
  scanf("%d",&e1.contact);
  printf("Enter address: ");
  scanf("%s",&e1.address);
  printf("Details of employee: ");
  printf("\nId: %d", e1.id);
  printf("\nName: %s", e1.name);
  printf("\nContact: %d",e1.contact);
  printf("\nAddress: %s", e1.address);
  return 0;
}

In C, use typedef with pointers.

We can also use typedef to create an alias for pointers.
Pointers are declared as follows:

float* p1;

Make an alias for float* as follows:

typedef float* fptr;

You can now declare float pointers as follows:

fptr p1, p2, p3;

To better understand this concept, consider the following example:

#include <stdio.h>
int main()
{
  typedef float* fptr;
  float n1 = 5.5,n2 = 20.65, n3 = 4.555;
  fptr p1 = &n1 , p2 = &n2, p3 = &n3;
  printf("n1: %.3lf, value: %.3lf, address: %p \n", n1, *p1, p1);
  printf("n2: %.3lf, value: %.3lf, address: %p \n", n2, *p2, p2);
  printf("n3: %.3lf, value: %.3lf, address: %p \n", n3, *p3, p3);

  return 0;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button