Table of contents
1.
Introduction
2.
What is Typedef keyword in C?
3.
Syntax of Typedef in C
4.
Example of Typedef in C
5.
Application of typedef in C
5.1.
1. typedef struct
5.1.1.
Syntax of typedef struct
5.1.2.
Example - Using typedef to define a name for a structure
5.2.
C
5.3.
2. Typedef with Pointers
5.3.1.
Example: Using typedef to define a name for pointer type.
5.4.
C
5.5.
3. typedef with Array
5.5.1.
Syntax
5.5.2.
Example: Using typedef to Define an Alias for Array
5.6.
C
6.
Difference Between C typedef vs #define
7.
Frequently Asked Questions
7.1.
What is the difference between typedef and struct tag in C?
7.2.
What does C typedef declare?
7.3.
Is typedef a macro in C?
7.4.
Why use typedef in C?
8.
Conclusion
Last Updated: Oct 8, 2024
Easy

Typedef in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C programming, the typedef keyword allows you to create an alias for an existing data type. It doesn't create a new data type but gives a new name to an existing one, making your code easier to read and manage.

Typedef in C

Also see : C Static Function and  Short int in C Programming

What is Typedef keyword in C?

Typedef is a keyword used in the c language to assign alternative names to current datatypes. It's used mainly with user-defined datatypes, while names of the datatypes come to be slightly complicated to apply in programs.

Syntax of Typedef in C

typedef <existing_name> <alias_name>  
You can also try this code with Online C++ Compiler
Run Code

Example of Typedef in C

typedef signed long slong;

Application of typedef in C

Some common applications of typedef in C:

  • Simplifying Complex Declarations: typedef helps to simplify complex declarations like pointers to functions, making the code more readable.
     
  • Portability: It allows for easy changes in data types across different platforms by defining a type alias, which can be modified as needed without altering the rest of the code.
     
  • Enhancing Code Readability: By giving meaningful names to types, typedef makes the code more understandable, especially in large projects.
     
  • Struct and Enum Usage: typedef is often used with structs and enums to avoid repeatedly writing struct or enum keywords, leading to cleaner and shorter code.

The idea of typedef can be carried out for defining a user-defined data type with a specific name and type. This typedef also can be used with structures of the c language.

1. typedef struct

Syntax of typedef struct

typedef struct
{
    type first_member;
    type sec_member;
    type thrid_member;
} nameOfType;
You can also try this code with Online C++ Compiler
Run Code

Example - Using typedef to define a name for a structure

  • C

C

#include<stdio.h>
#include<string.h>
typedef struct professor
{
   char p_name[50];
   int p_sal;
} prof;
void main(void)
{
   prof pf;
   printf("\n Enter Professor details: \n  \n");
   printf("\n Enter Professor name:\t");
   scanf("% s", pf.p_name);
   printf("\n Enter professor salary: \t");
   scanf("% d", &pf.p_sal);
   printf("\n Input done ! ");
}
You can also try this code with Online C Compiler
Run Code


Output

Enter Professor details: 
Enter Professor name: Aman
Enter professor salary: 65790 
Input done ! 


You can also read about the dynamic arrays in c.

2. Typedef with Pointers

Typedef can be used to present an alias name to pointers additionally. Right here, we've got a case in which using typedef is useful during pointer declaration.

In Pointers * binds to the right side and not on the left side.

int* x, y;
You can also try this code with Online C++ Compiler
Run Code

Using this declaration statement, we are simply declaring x as a pointer of type int, while y may be expressed as a simple int variable.

typedef int* IntPtr;
IntPtr x, y, z;
You can also try this code with Online C++ Compiler
Run Code

But if we use typedef as we've used in the example above, we can declare any number of pointers in a single statement.

Example: Using typedef to define a name for pointer type.

  • C

C

#include <stdio.h>

typedef int* IntPtr;

int main() {
int a = 10;
IntPtr p = &a;

printf("Value of a: %d\n", *p);
return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

Value of a: 10

3. typedef with Array

typedef can define an alias for array types, making code more readable and manageable, especially when dealing with complex array declarations.

Syntax

typedef existing_type new_name[array_size];


Example: Using typedef to Define an Alias for Array

  • C

C

#include <stdio.h>

typedef int IntArray[5];

int main() {
IntArray arr = {1, 2, 3, 4, 5};

for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

1 2 3 4 5 

Difference Between C typedef vs #define

  • typedef only creates aliases for types. #define can create aliases for both types and values, such as defining 1 as ONE or 3.14 as PI
     
  • The compiler interprets typedef. The preprocessor interprets #define.
     
  • You must end typedef with a semicolon. You should not use a semicolon with #define.
     
  • typedef defines a new type. #define performs text substitution, replacing occurrences with defined values.
     
  • typedef respects C's scope rules, limiting visibility to the scope in which it's defined. #define applies globally after being encountered by the preprocessor, without respecting scope rules.

Frequently Asked Questions

What is the difference between typedef and struct tag in C?

typedef creates an alias for a data type, making it easier to use complex structures. A struct tag is just a label for the structure itself, often used to define instances of that structure.

What does C typedef declare?

C typedef declares an alias for an existing data type, allowing you to use a simpler or more meaningful name for that type in your code.

Is typedef a macro in C?

No, typedef is not a macro. It's a keyword used to create type aliases, while macros are created using #define for text substitution.

Why use typedef in C?

Use typedef in C to create meaningful names for complex data types, improving code readability and maintainability by simplifying type declarations.

Conclusion

We learned that Typedef isn't always compulsory to apply. It's far used to reduce the complexity of declaration and offers you full permission to choose the name of your interest of the data type. You need to choose an alias name for the kind, which can explain a whole lot more approximately your variable working. Feel free to take any name of your new data type name. Find more such interesting questions on our practice platform Code360 if you want to learn more before jumping into practicing, head over to our library section for many such interesting blogs.

Live masterclass