Typedef
WHY does typedef exist?
WHAT it solves:
- Readability (
Ageinstead ofunsigned int) - Portability (change
typedef int Money;→typedef long Money;once) - Hiding complexity (function pointers, struct pointers)
HOW to read a typedef (the trick)
int x; // x is a variable of type int
typedef int Integer; // Integer is a NEW NAME for intSo to decode a complicated typedef:
- Mentally remove the word
typedef. - Pretend it's a normal variable declaration.
- Find the "variable name" — that identifier is your new type name.
- Whatever type that variable would have been is what the alias means.

Basic syntax & worked examples
Example 1 — Simple alias
typedef unsigned int uint;
uint count = 5; // same as: unsigned int count = 5;Why? uint is just a shorthand; the compiler substitutes unsigned int.
Example 2 — With struct (the most common real use)
struct Point { int x, y; };
typedef struct Point Point; // alias 'Point' for 'struct Point'
Point p; // no need to write 'struct Point p;'
p.x = 3;Why this step? In C, struct tags live in a separate namespace, so you normally must write struct Point. The typedef lets you drop the struct keyword. You can even combine declaration + typedef:
typedef struct { int x, y; } Point; // anonymous struct + aliasExample 3 — Pointer types
typedef int* IntPtr;
IntPtr a, b; // BOTH are int* ... a useful surprise!Why this matters: Compare to int* a, b; where only a is a pointer and b is a plain int. With the typedef, the * is baked into the alias, so both become pointers. This is a feature, not a bug.
Example 4 — Function pointer (hiding ugliness)
typedef int (*Operation)(int, int); // alias for "ptr to fn(int,int)->int"
int add(int a, int b){ return a + b; }
Operation op = add;
int r = op(2, 3); // r = 5Why? Function-pointer syntax is famously hard. Operation makes declarations like Operation table[4]; (an array of operations) instantly readable.
typedef vs #define — the crucial difference
Flashcards
Does typedef create a brand-new type or an alias?
Where does the new type name appear in a typedef?
Decode typedef char *String;
String becomes an alias for char * (pointer to char).In IntPtr a, b; with typedef int* IntPtr;, what are a and b?
int*.Key difference between typedef and #define?
typedef is a compiler-level type alias; #define is preprocessor text substitution and can misbehave with pointers/multiple declarators.Why does typedef struct Point Point; help?
Point p; instead of struct Point p;.Write a typedef for a pointer to a function taking two ints returning int.
typedef int (*Operation)(int,int);Trick to read a complicated typedef?
typedef, read it as a variable declaration; the variable's name is the new type, its type is the alias's meaning.Recall Feynman: explain to a 12-year-old
Imagine your friend's full name is "Maximilian Alexander". Too long to say every time! So you call him "Max". He's the same person — "Max" is just a nickname. typedef gives nicknames to types in C. Instead of saying "unsigned long integer" every time, you say "BigNumber". The computer still knows it's the long thing, but your code is shorter and clearer. It's only a name — it doesn't make a new kind of thing.
Connections
- Structures in C —
typedefis most useful for dropping thestructkeyword. - Pointers in C — pointer typedefs vs
#definepitfalls. - Function Pointers — typedef tames ugly fn-pointer syntax.
- Preprocessor and #define — contrast text substitution vs type aliasing.
- Linked Lists —
typedef struct node Node;makes node code clean.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, typedef ka simple matlab hai: kisi existing type ko ek naya, chhota naam (alias) dena. Yeh koi naya type nahi banata — bas nickname deta hai. Jaise unsigned long int likhna har baar bekaar lagta hai, to tum typedef unsigned long int BigNum; likh do, aur ab BigNum likhne se kaam ho jayega. Compiler internally usko wahi original type samajhta hai.
Padhne ka best trick: typedef word ko mentally hata do, aur usko ek normal variable declaration ki tarah padho. Jahan variable ka naam aata, wahi tumhara naya type name hai. Example: typedef char *String; — typedef hatao to bachta hai char *String;, yaani String ek char* variable hota — isliye String ab char* ka alias hai. Easy!
Sabse zyada use hota hai struct ke saath. Normally struct Point p; likhna padta, lekin typedef struct Point Point; ke baad sirf Point p; likho. Linked list, trees waghaira me yeh bahut clean code deta hai. Function pointers ke ugly syntax ko bhi typedef chhupa deta hai.
Ek important warning: typedef aur #define same nahi hain. #define sirf text replace karta hai (preprocessor), jabki typedef real type alias hai (compiler samajhta hai). Pointers ke saath #define INTPTR int* likh ke INTPTR a, b; karoge to sirf a pointer banega, b plain int — bug! typedef me dono pointer bante hain. Isliye types ke liye hamesha typedef use karo.