Lesson 51 of 60 – typedef in C
85%

typedef in C

The typedef keyword in C is used to create an alternative name for an existing data type. It can make complex declarations easier to read and use.

Note: typedef creates a new name or alias for an existing type. It does not create a completely new data type.

1. What is typedef?

typedef is a C keyword used to create another name for an existing data type.

typedef int Number;

Now Number can be used as an alias for int.

2. Why Use typedef?

The main purpose of typedef is to make declarations shorter, clearer, and easier to understand.

Without typedef:

unsigned long int value;

With typedef:

typedef unsigned long int ULong;

ULong value;

3. typedef Syntax

The general syntax is:

typedef existing_type new_name;

For example:

typedef int Number;

Here, Number becomes another name for int.

4. typedef with int

typedef int Number;

Number age;
Number marks;

Both age and marks are declared using the alias Number.

5. typedef with float

typedef float Price;

Price productPrice = 99.50f;
Price discount = 10.0f;

Here, Price is an alias for float.

6. typedef with char

typedef char Grade;

Grade grade = 'A';

printf("%c", grade);

The alias Grade can be used wherever a char type is appropriate.

7. typedef with unsigned int

typedef unsigned int ID;

ID studentId = 101;

This can make a program easier to understand because the alias describes the intended purpose of the value.

8. typedef with Structure

One of the most common uses of typedef is with structures.

typedef struct
{
    int id;
    char name[50];
} Student;

Now we can declare a structure variable simply as:

Student student1;

9. Structure Without typedef

Without typedef, a structure variable normally needs the struct keyword.

struct Student
{
    int id;
    char name[50];
};

struct Student student1;

With typedef, the declaration can be shorter.

typedef struct
{
    int id;
    char name[50];
} Student;

Student student1;

10. typedef with Named Structure

typedef struct Student
{
    int id;
    char name[50];
} Student;

Now both struct Student and the typedef name Student can be used to refer to the structure type.

11. typedef with Union

The typedef keyword can also simplify union declarations.

typedef union
{
    int number;
    float price;
    char grade;
} Data;

Now we can write:

Data value;

12. typedef with enum

An enum can also be given a shorter type name using typedef.

typedef enum
{
    LOW,
    MEDIUM,
    HIGH
} Level;

Now a variable can be declared as:

Level currentLevel = HIGH;

13. typedef with Arrays

A typedef can also create an alias for an array type.

typedef int Numbers[5];

Numbers values = {10, 20, 30, 40, 50};

Here, Numbers represents an array of five integers.

14. typedef with Pointer

A typedef can create an alias for a pointer type.

typedef int *IntPtr;

int number = 100;

IntPtr ptr = &number;

Here, IntPtr represents int *.

15. typedef with Multiple Variables

typedef int Number;

Number a = 10;
Number b = 20;
Number c = 30;

Once the alias is created, it can be reused throughout the program.

16. typedef Improves Readability

Meaningful aliases can make source code easier to understand.

typedef unsigned int StudentID;

StudentID id = 101;

The name StudentID communicates the purpose of the variable more clearly than simply writing unsigned int.

17. typedef Does Not Create a New Type

A typedef name is an alias for an existing type.

typedef int Number;

Number x = 10;
int y = 20;

Number is an alternative name for int. It does not create a completely separate integer type.

18. typedef and const

A typedef can be combined with type qualifiers such as const.

typedef int Number;

const Number value = 100;

The variable value cannot be modified after initialization through normal assignment.

19. typedef in Function Parameters

Typedef names can make function declarations easier to read.

typedef int Number;

void display(Number value)
{
    printf("%d", value);
}

The function parameter uses the alias Number.

20. typedef for Function Pointers

One useful advanced application of typedef is creating an alias for a function pointer type.

typedef int (*Operation)(int, int);

Now Operation represents a pointer to a function that takes two integers and returns an integer.

21. Function Pointer typedef Example

#include <stdio.h>

typedef int (*Operation)(int, int);

int add(int a, int b)
{
    return a + b;
}

int main()
{
    Operation op = add;

    printf("%d", op(10, 20));

    return 0;
}

The typedef makes the function pointer declaration easier to read.

22. typedef with Nested Structures

typedef struct
{
    int day;
    int month;
    int year;
} Date;

typedef struct
{
    int id;
    char name[50];
    Date joiningDate;
} Employee;

Here, the Date typedef is used as a member type inside Employee.

23. typedef with Multiple Structures

typedef struct
{
    int id;
    char name[50];
} Student;

typedef struct
{
    int id;
    float salary;
} Employee;

Student s1;
Employee e1;

Each typedef provides a convenient name for its corresponding structure.

24. typedef in Large Programs

In larger programs, typedef can simplify complicated declarations and make common data types easier to reuse.

For example:

typedef unsigned long ID;

typedef struct
{
    ID id;
    char name[50];
} Student;

This creates reusable and descriptive type names.

25. Common Mistake: Confusing typedef with Variable Declaration

A typedef statement creates a type alias. It does not normally create an ordinary variable.

typedef int Number;

This creates the alias Number. A variable is created separately:

Number age = 20;

26. Common Mistake with Pointer typedef

Be careful when using pointer aliases because the alias itself may already include the *.

typedef int *IntPtr;

IntPtr p1;
IntPtr p2;

Both p1 and p2 are pointers to int.

27. Complete typedef Structure Program

#include <stdio.h>

typedef struct
{
    int id;
    char name[50];
    float marks;
} Student;

int main()
{
    Student student;

    student.id = 101;

    printf("Enter name: ");
    scanf("%49s", student.name);

    printf("Enter marks: ");
    scanf("%f", &student.marks);

    printf("\nStudent ID = %d\n", student.id);
    printf("Name = %s\n", student.name);
    printf("Marks = %.2f\n", student.marks);

    return 0;
}

This program uses typedef to create the Student type.

28. typedef and Code Maintenance

Using meaningful typedef names can make a program easier to maintain. If a complex type is used many times, a single alias can make declarations shorter and more consistent.

typedef unsigned long long StudentID;

StudentID id1;
StudentID id2;

29. Practical Uses of typedef

The typedef keyword is commonly useful for:

  • Structure aliases
  • Union aliases
  • Enum aliases
  • Pointer aliases
  • Array aliases
  • Function pointer aliases
  • Descriptive application-specific types
  • Simplifying complex declarations

30. Practice Program

Create a C program that:

  1. Creates a typedef named StudentID for int.
  2. Creates a typedef structure named Student.
  3. Adds ID, name, and marks to the structure.
  4. Creates a typedef enum named Status.
  5. Adds ACTIVE and INACTIVE values.
  6. Creates a student variable using the typedef structure.
  7. Stores student information.
  8. Uses the enum to store the student's status.
  9. Displays all the information.

This exercise combines typedef with structures and enumerations.

📌 Key Points

  • typedef creates an alternative name for an existing type.
  • It can make complex declarations shorter and easier to read.
  • It does not create a completely new data type.
  • typedef is commonly used with structures.
  • It can also be used with unions and enums.
  • Pointer types can be given aliases using typedef.
  • Array types can also be given aliases.
  • Function pointer declarations can be simplified using typedef.
  • Meaningful typedef names can improve code readability.
  • Typedef aliases can be reused throughout a program.

🧠 Quick Quiz

Question: What is the main purpose of typedef in C?