🟡 Intermediate  ·  Lesson 32

typedef in C

What is typedef?

Some type names in C are long and repetitive — unsigned long int, struct Employee, or a tangled function-pointer type. typedef lets you give any existing type a short, readable alias, so you can use the short name everywhere instead.

Important: typedef does not create a new type. It only creates a nickname for a type that already exists. The variable behaves exactly the same — it is just easier to write and read.

A basic alias

C Language
#include <stdio.h>
typedef unsigned int uint;   // uint now means unsigned int

int main() {
    uint age = 25;
    printf("Age = %u\n", age);
    return 0;
}
Output:
Age = 25

Here uint is simply a friendlier name for unsigned int. Both mean the same thing to the compiler.

typedef with struct

This is the most common use. Normally you repeat the struct keyword every time; typedef removes it.

C Language
#include <stdio.h>
typedef struct {
    char name[20];
    int  age;
} Person;                 // Person is the alias

int main() {
    Person p = {"Aman", 22};
    printf("%s is %d\n", p.name, p.age);
    return 0;
}
Output:
Aman is 22

Thanks to typedef, you write Person p; instead of struct Person p; — cleaner and closer to how other languages name types. (New to structures? See structure vs union.)

typedef for pointers and function pointers

typedef really shines when a type is hard to read. Function pointers are the classic example.

C Language
#include <stdio.h>
typedef int (*Operation)(int, int);   // alias for the messy type

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

int main() {
    Operation op = add;        // clean and readable
    printf("Sum = %d\n", op(5, 6));
    return 0;
}
Output:
Sum = 11

Without the typedef you would have to write the full int (*op)(int, int) every time. (See function pointers for the raw syntax.)

typedef vs #define

Both can name things, but they are not the same. #define is blind text replacement; typedef understands types.

Pointtypedef#define
Handled byCompilerPreprocessor
Understands typesYesNo (plain text)
Works with pointers safelyYesOften buggy
Scope rulesFollows normal scopeText everywhere below it
⚠️ A subtle trap

#define PTR int* then PTR a, b; makes only a a pointer. With typedef int* PTR;, both a and b are pointers. This is why typedef is safer for types.

Common mistakes

  • Thinking typedef creates a new type — it only makes an alias.
  • Using #define for pointer types and getting surprising declarations.
  • Putting the alias name in the wrong place (it goes at the end of the typedef).
  • Overusing typedef for simple types, which can hide what a variable really is.
🏋️ Practice

Define a typedef struct named Book with a title and a price, create two books, and print them. Then rewrite one declaration the long way (struct ... ) to prove both forms are identical.

Summary

  • typedef gives an existing type a short, readable alias — no new type is created.
  • With structs it removes the repeated struct keyword (Person p;).
  • It makes pointer and function-pointer types far easier to read.
  • Prefer typedef over #define for naming types — it is type-aware and safer.

Frequently Asked Questions

What is typedef in C?
typedef creates a new, shorter name (an alias) for an existing type. It does not create a new type — it just gives an existing one a more readable label. For example typedef unsigned long ulong; lets you write ulong instead of unsigned long.
What is the difference between typedef and #define?
typedef is handled by the compiler and understands real types, so it works correctly even with pointers and complex declarations. #define is a plain text replacement done by the preprocessor with no type awareness, which can behave unexpectedly. For naming types, typedef is the safer choice.
Why is typedef used with struct in C?
Without typedef you must write struct Point p; every time. With typedef struct { ... } Point; you can just write Point p;. It removes the repeated struct keyword and makes the code cleaner and easier to read.
Does typedef create a new data type?
No. typedef only creates an alias for an existing type — the underlying type is unchanged. A variable declared with the alias is exactly the same type as one declared the long way, so they are fully interchangeable.
Can typedef make function pointers easier to read?
Yes, this is one of its best uses. Function pointer syntax is hard to read, so a typedef like typedef int (*Operation)(int, int); lets you then declare variables simply as Operation op; instead of repeating the full messy syntax each time.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.