typedef in c

Typedef in C

typedef is a reserved keyword used in c language to specify alternative names to previously existing data types. Its basic use is for aliasing. An alias is a temporary name assigned to something when the original name is too complex to be used otherwise. typedef allows the user to assign an alias to the data types which may or may not be user-defined. It is used to make complex names easier. Aliases can be defined locally for any function or globally for the entire program as well. An important thing to remember is that this keyword adds a new name for some existing data type but does not create a new type.

The syntax for using the typedef keyword is:

typedef existingName newName;

For example:

typedef unsigned char uchar;

The above statement creates an identifier uchar for defining unsigned char datatype. In the entire code now, any unsigned character can be defined by using uchar instead of writing unsigned char. Thus, it can be said that typedef is used to simplify the commands as per our needs.

typedef and structures

Apart from the existing data types, typedef can also be used to alias user-defined datatypes such as structures. While using typedef with structures, there is a slight difference in the syntax.

typedef struct originalName {
type member1;
type member2;
type member3;
} aliasName;

We can also use typedef with unions instead of structures. In that case everything will remain the same only the keyword struct will be replaced with the keyword union.

typedef union originalName {
type member1;
type member2;
type member3;
} aliasName;

An example of typedef with structure will help understand the concept better.

typedef struct student {
int id;
char name[20];
float percentage;
} st;

int main() {
st record;
record.id=1;
strcpy(record.name, "Aakhya");
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;
}

In this case, the structure student has been given another name st. It allows the programmer to code using lesser complex words. The output of the given code is:

typedef in C 1

This is equivalent to st = “struct student”.

typedef and pointers

typedef can also be used with pointers. There is no difference in the syntax when using typedef with pointers. Example:

typedef int * iptr;

This statement defines iptr as an alias of the pointer to int. In simpler words this means

iptr p;
int *p;

these two statements are equivalent. But if we use typedef with pointers, we can declare any number of pointers in a single statement.

typedef int* intPtr;
intPtr x, y, z;

To understand it better, let us take an example

#include<stdio.h>
typedef int * iptr;
int main()
{
int y = 8;
iptr x; // same as int *x
x = &y;
printf("Address of x is: %u\n", x);
printf("Value stored at address of x is: %d\n", *x);
return 0;
}

The expected output will be:

typedef in C 2

typedef Vs #define

typedef in C 3

  • The major difference between the two keywords is that typedef can only be used to alias names whereas #define can also specify alias values. That means you can specify pi as 3.14 using #define but not using typedef.
  • Another significant difference is that typedef is interpreted by the compiler whereas #define is interpreted by the preprocessor.
  • There is also a difference in the syntax of these two keywords. #define statements do not end in a semicolon while typedef statements end with a semicolon.

Advantages of typedef

  • It makes the program more readable and easier to understand.
  • The use of complex names is reduced however the global space may sometimes become cluttered.
nv-author-image

Anurag batra

A Tech Enthusiast who loves to work on his craft and make unique stuff From his Programming Skill at a same time Sharing that Knowledge with the outside world to help others.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments