Typedef
5.1.26· Coding › C Programming
typedef exist kyun karta hai?
YEH kya solve karta hai:
- Readability (
Ageinstead ofunsigned int) - Portability (sirf ek jagah badlo
typedef int Money;→typedef long Money;) - Complexity chhupana (function pointers, struct pointers)
typedef ko kaise padhein (trick)
int x; // x is a variable of type int
typedef int Integer; // Integer is a NEW NAME for intToh ek complicated typedef decode karne ke liye:
- Mentally
typedefword ko hatao. - Samjho jaise yeh normal variable declaration hai.
- "Variable name" dhundho — woh identifier tumhara naya type name hai.
- Joh type us variable ki hoti — wahi alias ka matlab hai.

Basic syntax aur worked examples
Example 1 — Simple alias
typedef unsigned int uint;
uint count = 5; // same as: unsigned int count = 5;Kyun? uint sirf ek shorthand hai; compiler unsigned int substitute kar deta hai.
Example 2 — struct ke saath (sabse 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;Yeh step kyun? C mein, struct tags ek alag namespace mein rehte hain, toh normally struct Point likhna padta hai. typedef tumhe struct keyword chhod dene deta hai. Tum declaration + typedef combine bhi kar sakte ho:
typedef struct { int x, y; } Point; // anonymous struct + aliasExample 3 — Pointer types
typedef int* IntPtr;
IntPtr a, b; // DONO int* hain ... ek useful surprise!Yeh kyun matter karta hai: Compare karo int* a, b; se jahan sirf a pointer hai aur b plain int hai. Typedef ke saath, * alias mein bake-in ho jaata hai, toh dono pointers ban jaate hain. Yeh ek feature hai, bug nahi.
Example 4 — Function pointer (ugliness chhupana)
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 = 5Kyun? Function-pointer syntax mushkil hota hai. Operation se Operation table[4]; jaisi declarations (operations ka array) turant readable ban jaati hain.
typedef vs #define — crucial difference
Flashcards
Kya typedef bilkul naya type banata hai ya alias?
Typedef mein naya type name kahan aata hai?
typedef char *String; decode karo
String char * (pointer to char) ka alias ban jaata hai.typedef int* IntPtr; ke saath IntPtr a, b; mein a aur b kya hain?
int* hain.typedef aur #define mein key difference?
typedef compiler-level type alias hai; #define preprocessor text substitution hai aur pointers/multiple declarators ke saath galat behave kar sakta hai.typedef struct Point Point; kyun help karta hai?
struct Point p; ki jagah Point p; likhne deta hai.Do ints leke int return karne wale function ka pointer typedef likho.
typedef int (*Operation)(int,int);Complicated typedef padhne ki trick?
typedef hatao, ise variable declaration ki tarah padho; variable ka naam naya type hai, uski type alias ka matlab hai.Recall Feynman: ek 12-saal ke bacche ko samjhao
Socho tumhare dost ka full naam hai "Maximilian Alexander". Har baar itna lamba bolna mushkil hai! Toh tum use "Max" bulate ho. Woh same insaan hai — "Max" sirf ek nickname hai. typedef C mein types ko nicknames deta hai. Har baar "unsigned long integer" bolne ki jagah, tum "BigNumber" bolte ho. Computer ko phir bhi pata hai ki woh lambi wali cheez hai, lekin tumhara code chhota aur clearer hota hai. Yeh sirf ek naam hai — yeh koi naya kind ki cheez nahi banata.
Connections
- Structures in C —
typedefsabse zyada useful haistructkeyword drop karne ke liye. - Pointers in C — pointer typedefs vs
#defineke pitfalls. - Function Pointers — typedef ugly fn-pointer syntax ko tame karta hai.
- Preprocessor and #define — text substitution vs type aliasing ka contrast.
- Linked Lists —
typedef struct node Node;node code ko clean banata hai.