Typedef Struct in C || Typedef Struct C Programming Language

Typedef Struct in C  || Typedef Struct C Programming Language

In C there are many built in data types. Sometimes, the built in data types are not enough to perform the required tasks. In that case, some custom data type can be built to meet the necessary requirements. The following are some build in data types in c:
  ü  Structure
  ü  Union
  ü  Typedef
Today we are going to discuss about typedef struct in c. So let’s start—
Typedefs is a way of renaming built in data type (char, int) as well as custom data type(struct, union). It allows us to define types with a different name – which can come in handy when dealing with structs and pointers. typedef makes the code short and improves readability. By the help of typedefwe can get rid of the long definition of a point structure. It is like an alias of struct. It is a keyword which is used to give a new symbolic name for the existing name in a C program
typedef <existing_name> <alias_name>


typedef char word;
typedef for built in data type:
We can use typedef for built in data types (i.e. int, float). For example:
typedef int Int16;
typedef float F32;

Then we can declare those variables like this:

Int16 age, id;
F32 salary;
Typedef custom data type:
We can use typedef as custom data type (union, enumeraton, structure) as well as built in data type.

Typedef Structure:
We have a student structure like following
struct student
 int id;
 int course_code;
 int section;

We can typedef it to another name (learner).

typedef struct student learner;
Code without typedef struct c 

struct programiz {
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
struct programiz var; 
var.town = "Agra";

Code using tyepdef struct in c
typedef struct programiz{
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
addr var1;
var.town = "Agra";

Instead of using the struct programiz every time you need to declare struct variable, you can simply use addr, the typedef that we have defined.

Declaring Typedef Struct in C Example1:
Consider the following structure:

struct student
         int mark [2];
         char name [10];
         float average;

We can declare the above variable in two ways.
1st way:

struct student record;       /* for normal variable */
struct student *record;     /* for pointer variable */

2nd way:

typedef struct student status;

Example 2:

// Structure using typedef:
#include <stdio.h>
#include <string.h>
typedef struct student 
  int id;
  char name[20];
  float percentage;
} status;
int main() 
  status record;
  strcpy(record.name, "Himu");
  record.percentage = 86.5;
  printf(" Id is: %d n", record.id);
  printf(" Name is: %s n", record.name);
  printf(" Percentage is: %f n", record.percentage);
  return 0;

When above code is executed, our output looks like−

Id is: 1
Name is: Himu
Percentage is: 86.500000
Typedef and Pointers

We can also use typedef with pointer. In Pointers * binds to the right and not on the left.

int* x, y;

Above we are actually declaring x as a pointer of type int, whereas y will be declared as a plain int variable. But if we use typedef, we can declare any number of pointers in a single statement.

typedef int* IntPtr;
IntPtr x, y, z;
typedef vs #define
#define is also used to define the aliases for various data types which is similar to typedef but have some differences −
  • #define can be used to define alias for values as well, q., you can define 1 as ONE etc. Where typedef is limited to giving symbolic names
  • #define statements are processed by the pre-processor, where typedef interpretation is performed by the compiler
Examples of how to use #define in a program:
#include <stdio.h>
#define TRUE  1
#define FALSE 0
int main( ) {
   printf( "Value of TRUE : %dn", TRUE);
   printf( "Value of FALSE : %dn", FALSE);

   return 0;

When above code is executed, our output looks like−

Value of TRUE : 1
Value of FALSE : 0

If you want to more about structure in c,then just click Here. So, guys, that’s all about typedef struct in c. Later we will discuss another tutorial on c programming. Till then, take care. Happy Coding.

Post a Comment