Exercises — Typedef
5.1.26 · D4· Coding › C Programming › Typedef
Ek trick yaad rakho jo har problem unlock karti hai (from Typedef):
Level 1 — Recognition
Goal: kya aap bata sakte hain ki ek typedef kya naam deta hai, bina kuch run kiye?
L1.1
typedef unsigned char Byte;Q: Byte kaunsi existing type ko naam deta hai?
Recall Solution
typedef cover karo → unsigned char Byte; ek variable Byte declare karta hai type unsigned char ka.
Isliye Byte, ==unsigned char== ka alias hai.
Ek chhota sanity check: unsigned char values se tak hold karta hai (yaani ), isliye "Byte" ek fitting nickname hai.
L1.2
typedef double Real;
Real pi = 3.14;Q: Iske baad, kya Real pi = 3.14; legal hai, aur pi ki actual type kya hai?
Recall Solution
Haan, legal hai. Real hai hi double, toh Real pi = 3.14; bilkul double pi = 3.14; ke jaisa hai.
typedef ek naam banata hai, koi nayi cheez nahi — pi ek plain double hai.
L1.3
typedef char *String;Q: Kya String char ka alias hai, ya char * ka?
Recall Solution
typedef cover karo → char *String;. * naam String ke saath bind hota hai, char ke saath nahi. Toh String ek variable hoga type char * ka.
Isliye String = ==char *== (pointer to char), char nahi.
Level 2 — Application
Goal: ek typedef use karke chhota, sahi code likho.
L2.1
Diya hua hai:
struct Point { int x, y; };
typedef struct Point Point;Q: Ek line likho jo Point naam ka ek variable p declare kare aur p.x = 3, p.y = 4 set kare.
Recall Solution
Point p;
p.x = 3;
p.y = 4;Kyun kaam karta hai: alias ke bina aapko struct Point p; likhna padta kyunki C mein struct tag Point ek alag namespace mein rehta hai. typedef Point ko ordinary type namespace mein daal deta hai, toh aap struct keyword hata sakte hain. Dekho Structures in C.
L2.2
typedef int* IntPtr;
IntPtr a, b;Q: a aur b ki types kya hain?
Recall Solution
Dono a aur b int* hain.
* alias IntPtr ke andar baka hua hai. Jab aap IntPtr a, b; likhte hain toh har variable independently poori aliased type paata hai. Toh a hai int* aur b hai int*.
L2.3
typedef unsigned int uint;
uint x = 5;
uint y = 4294967295; /* 2^32 - 1 */Q: Ek system par jahan unsigned int 32 bits ka hai, x + 1 kya hai aur y + 1 kya hai?
Recall Solution
uint hai unsigned int, toh arithmetic modulo wrap around karti hai.
x + 1 = 6.- Value sabse bada representable value hai, toh
y + 1wrap karke ho jaata hai. Alias ne kuch nahi badla behaviour mein — yeh abhi bhiunsigned inthai.
Level 3 — Analysis
Goal: differences predict karo aur explain karo kyun.
L3.1
#define INTPTR int*
typedef int* IntPtr;
INTPTR a, b;
IntPtr c, d;Q: a, b, c, d mein se har ek ko int ya int* classify karo.
Recall Solution
#define ek dumb text replacement hai preprocessor ke zariye. INTPTR a, b; literally ban jaata hai:
int* a, b; // a is int*, b is intToh a = int*, b = int.
typedef ek real type alias hai jo compiler samajhta hai. IntPtr c, d; har variable ko poora type deta hai:
c=int*,d=int*.
Summary: a = int*, b = int, c = int*, d = int*. Yeh crucial typedef vs #define split hai Pointers in C se.
L3.2
Cover-typedef trick se yeh declaration decode karo:
typedef int (*Operation)(int, int);Q: Operation kaunsi type hai?
Recall Solution
typedef cover karo → int (*Operation)(int, int);.
Declarator ko identifier se baahir ki taraf padhो:
Operationnaam hai.(*Operation)— parentheses*ko pehle bind karne ke liye force karte hain:Operationkisi cheez ka pointer hai.(...)(int, int)— woh cheez ek function hai jo(int, int)leta hai.- Pehle wala
intreturn type hai.
Isliye Operation = "pointer to a function jo do ints leta hai aur int return karta hai". Dekho Function Pointers.
L3.3
typedef int Money;
Money price = 100; // 100 rupeesBaad mein project ko cents (fractions) chahiye, toh koi alias badal deta hai:
typedef double Money;Q: Kitni usage code lines badalni padengi, aur Money price = 100; ab kya value hold karega?
Recall Solution
Zero usage lines badlengi — yahi toh portability ka poora point hai. Sirf ek typedef line badli.
Change ke baad Money hai double, toh Money price = 100; stores 100.0 (ek double). price / 3 ab truncated integer 33 ki jagah fractional result dega.
Yeh dikhata hai ki ek policy type (jaise "hum money kaise store karte hain") ko ek naam ke peeche alias karna powerful kyun hai: ek baar badlo, sab kuch follow karta hai.
Level 4 — Synthesis
Goal: khud multi-piece type designs banao.
L4.1
Q: Operations (from L3.2) ki array of 4 ke liye ek typedef likho, array type ko apna alias OperationTable4 do. Phir dikhao ki teesri stored operation ko (2, 3) par kaise call karein.
Recall Solution
typedef int (*Operation)(int, int); // from L3.2
typedef Operation OperationTable4[4]; // NEW alias: array of 4 Operations
OperationTable4 table; // 'table' is an array of 4 function pointers
// suppose table[2] = add; where int add(int,int){...}
int r = table[2](2, 3); // calls add(2,3)Naye typedef ko kaise padhen: typedef cover karo → Operation OperationTable4[4]; OperationTable4 ko array of 4 Operations declare karta hai. Toh identifier OperationTable4 alias ban jaata hai, aur iska matlab hai "array of 4 function pointers".
Alias kyon kaam aata hai: kisi bhi alias ke bina, yeh type likhi jaati hai int (*table[4])(int, int); — almost unreadable. Do typedefs se, OperationTable4 table; plain English jaisi padhi jaati hai. Teesri stored operation hai table[2]; add(2,3) call karna jahan int add(int a,int b){return a+b;} gives ==5==.
L4.2
Q: Ek anonymous struct use karke, ek single typedef likho jo Vec2 ko float x, y; wala type banaye. Phir Vec2 v = {1.5f, 2.5f}; declare karo aur v.x + v.y compute karo.
Recall Solution
typedef struct { float x, y; } Vec2;
Vec2 v = {1.5f, 2.5f};
float s = v.x + v.y; // s = 4.0Anonymous kyun? Hume kabhi struct ko tag se refer nahi karna, sirf alias Vec2 se, isliye tag naam skip kar dete hain. v.x + v.y = 1.5 + 2.5 = ==4.0==. Dekho Structures in C.
L4.3
Q: Ek linked list ke liye typedef likho taaki Node ka matlab struct node ho, jisse self-referential pointer next allow ho sake. Phir batao ki sizeof(Node) roughly kya hoga 64-bit system par (maano int = 4 bytes, pointer = 8 bytes, padding se pehle).
Recall Solution
typedef struct node {
int data;
struct node *next; // must use the TAG here, alias not visible yet
} Node;Tag node abhi bhi kyun chahiye: struct body ke andar alias Node abhi exist nahi karta (woh typedef naam sirf closing brace ke baad usable hota hai), toh self-reference ke liye struct node * use karna padta hai.
Raw member sizes: int data = 4 bytes, struct node *next = 8 bytes → 12 bytes of data. Alignment ke saath, compiler 8 ka multiple banane ke liye pad karta hai, giving sizeof(Node) = 16 bytes. Dekho Linked Lists aur Pointers in C.
Level 5 — Mastery
Goal: edge par reason karo — degenerate aur tricky cases.
L5.1
Q: Kya yeh legal hai, aur agar hai toh kya karta hai?
typedef int Integer;
typedef Integer Whole;
Whole n = 7;Recall Solution
Legal hai. Ek typedef doosre alias ko alias kar sakta hai. Chain: Whole → Integer → int. Toh Whole n = 7; sirf int n = 7; hai. Aliasing transitive hai; kisi bhi step par koi "new type" nahi banti, sirf names int ki taraf point karte hain.
L5.2
Q: Diya hua hai
typedef int Arr[3];
Arr a;a ki type kya hai, aur sizeof(a) kya hai (4-byte int ke saath)?
Recall Solution
typedef cover karo → int Arr[3]; Arr ko array of 3 ints declare karta hai. Toh alias Arr ka matlab hai "array of 3 int". Arr a; isliye a ko array of 3 int banata hai.
sizeof(a) = 3 × 4 = ==12== bytes.
Yeh array typedefs ki surprising power (aur danger) hai: array-ness naam ke andar chhup jaati hai.
L5.3
Q: typedef int Arr[3]; ke saath, ek function void f(Arr x) consider karo. Kya f ke andar x array of 3 ints ki tarah behave karta hai, ya pointer ki tarah? 64-bit system par f ke andar sizeof(x) kya hai?
Recall Solution
Yeh ek degenerate/edge case hai. C mein, koi bhi array parameter — chahe typedef ke peeche chhupa ho — decay ho jaata hai pointer mein. Toh void f(Arr x) actually hai void f(int *x).
f ke andar, x ek pointer hai, toh sizeof(x) = ==8== bytes (64-bit par pointer size), 12 nahi.
Lesson: alias array type chhupata hai, lekin array-to-pointer decay rule abhi bhi function boundaries par apply hoti hai — naam ne language ka behaviour nahi badla, sirf spelling badli.
How the levels build
Yeh ladder deliberate hai — har level neeche wali rung reuse karta hai aur exactly ek nayi idea add karta hai:
| Level | Jo nayi idea add karta hai | Ek cheez jo upar carry karni hai |
|---|---|---|
| L1 Recognition | Alias kya naam deta hai, spot karo | Alias ek naam hai, kuch nahi |
| L2 Application | Alias use karo (struct, pointer, wraparound) |
Behaviour = underlying type ka behaviour |
| L3 Analysis | typedef vs #define |
Compiler alias ≠ text substitution |
| L4 Synthesis | fn-ptr arrays, structs, list nodes banao | Aliases nest aur self-reference kar sakte hain |
| L5 Mastery | Array aliases, chains, parameter decay | Naam kabhi language rules nahi badalta |
Recall
Ek rule jo har level survive karta hai ::: typedef ek naam banata hai, kabhi naya type nahi — behaviour (wraparound, decay, size) bilkul underlying type jaisa hi hai.
Arr x as a parameter ka size 12 nahi 8 kyun hai ::: array parameters C mein pointers mein decay karte hain, chahe typedef se ho.
Connections
- Typedef — parent note (theory jisko yeh exercises drill karti hain).
- Structures in C · Pointers in C · Function Pointers · Linked Lists — woh real types jinhe humne alias kiya.
- 🇮🇳 5.1.26 Typedef (Hinglish)