5.1.26 · D5 · HinglishC Programming

Question bankTypedef

1,636 words7 min read↑ Read in English

5.1.26 · D5 · Coding › C Programming › Typedef

Shuru karne se pehle, teen words jinpar hum constantly lean karte hain. Ek type woh label hai jo C data pe lagata hai taaki usse uski size aur kaun se operations legal hain pata chale (jaise int, char *). Ek declarator declaration ka woh hissa hai jo kisi cheez ko naam deta hai, possibly *, [], ya () mein wrap hokar — jaise int *p mein, declarator *p hai. Ek tag namespace ek alag list hai jo C un names ke liye rakhta hai jo struct/union/enum ke baad likhe jaate hain, ordinary names se alag rakhi jaati hai.


True ya false — justify karo

Har answer "true/false kyunki…" hoga — reasoning hi point hai.

typedef ek bilkul naya data type banata hai jo original se alag hota hai.
False. Yeh sirf ek alias banata hai; aliased naam aur original poori tarah se interchangeable hain, aur compiler unhe har check mein same type maanta hai.
typedef int Age; Age a = 5; int b = a; bina kisi cast ke compile hota hai.
True. Age hai hi int, toh unke beech assign karna int ko int assign karna hai — koi conversion nahi, koi warning nahi.
typedef C preprocessor dwara process hota hai, jaise #define.
False. typedef ko compiler real parsing ke dauran handle karta hai; #define blind text substitution hai jo preprocessor dwara pehle ki jaati hai.
typedef int* IntPtr; IntPtr a, b; ke saath dono a aur b pointers hain.
True. * alias mein hi bake ho jaata hai, toh har declared naam poora pointer type inherit karta hai — unlike raw int* a, b;.
typedef struct Point Point; illegal hai kyunki naam Point do baar use hua hai.
False. Dono Point alag namespaces mein rehte hain — pehla ek struct tag hai, doosra ek ordinary type naam — toh dono collide nahi karte.
typedef runtime par original type directly use karne se zyada memory use karta hai.
False. Yeh purely ek compile-time naam hai; zero runtime cost, zero extra bytes. Generated machine code identical hota hai.
typedef unsigned long ULong; phir use typedef unsigned int ULong; mein change karna har variable ko update kar deta hai jo ULong ke saath declare hua tha.
True. Woh single-point change exactly portability ka benefit hai — har use next compile par nayi meaning re-read karta hai.
Tum ek type ko ek naam se typedef kar sakte ho aur phir us naam ko dobara kisi teesre naam se typedef kar sakte ho.
True. Aliases chain karte hain; typedef int A; typedef A B; B ko A ka alias banata hai jo int ka alias hai — sab int ka matlab rakhte hain.
typedef declarations C scope rules maante hain.
True. Kisi function ke andar ek typedef us block mein local hota hai, unlike #define jo scope ko bilkul ignore karta hai aur neeche har jagah leak hota hai.

Error dhundho

Har snippet mein ek conceptual flaw hai. Use naam do.

#define INTPTR int* phir INTPTR a, b; — kya b ek pointer hai?
Nahi — yeh classic trap hai. Text substitution se int* a, b; milta hai, toh sirf a pointer hai aur b plain int hai. Agar dono pointers chahiye toh typedef use karo.
typedef int; — kya galat hai?
Koi naya naam nahi hai — declarator (woh "variable naam" slot) missing hai, toh alias karne ke liye kuch bhi nahi. Ek typedef ko ek fresh identifier dena zaroori hai.
typedef struct { int x; } ; baad mein Point p; ke roop mein use kiya — Point kyun fail hota hai?
Anonymous struct ko koi alias naam nahi diya gaya, toh Point kabhi define hi nahi hua. Yeh typedef struct { int x; } Point; hona chahiye tha.
typedef int Meter; Meter m = 3; Meter += 1; — galti dhundho.
Type naam Meter par arithmetic nahi ki ja sakti; tum variable m par operate karte ho. Yeh m += 1; hona chahiye. Type-vs-value distinction ke liye Pointers in C dekho.
typedef Node struct node; — yeh compile kyun nahi hoga?
Order ulta hai. typedef ek variable declaration ki tarah padhta hai: pehle existing type, baad mein naya naam — typedef struct node Node;.
typedef int (Operation)(int,int); function pointer ki tarah intended — kya missing hai?
*. Jaise likha hai, Operation ek function type ko alias karta hai, pointer to function ko nahi. Tumhe typedef int (*Operation)(int,int); chahiye — Function Pointers dekho.
#define uint unsigned int phir uint * p; kisi struct ke andar — typedef ke muqable koi hidden risk?
#define scope ignore karta hai, toh yeh macro file mein neeche har jagah uint ko replace karta hai, chahe unrelated code mein bhi; ek typedef apne declared scope tak confined rehta.

Why questions

typedef ek clean alias X = Y; form ki jagah variable-declaration syntax reuse karta hai. Yeh actually convenient kyun hai?
Kyunki wahi declarator rules jo complex variable types (pointers, arrays, function pointers) banate hain woh phir complex type names bhi free mein banate hain — ek grammar, seekhne ke liye koi doosra syntax nahi.
typedef struct Point Point; baad mein struct keyword drop karne deta hai kyun?
Alias Point ordinary type namespace mein rehta hai, toh Point p; likhne par woh directly mil jaata hai, jabki bare tag Point ke liye tag namespace tak pahunchne ke liye struct prefix chahiye.
Function-pointer types ke liye typedef ko #define se prefer kyun kiya jaata hai?
Function-pointer declarators complex aur position-sensitive hote hain; typedef unhe ek baar real syntax ke roop mein parse karta hai, jabki #define ki dumb substitution tab break hoti hai jab naam declarator ke andar hona zaroori ho (jaise int (*NAME)(int)).
Pointer typedef ka * "bake in" karna logon ko kabhi kabhi surprise kyun karta hai?
Readers expect karte hain ki * declaration mein variable se bind hoga, lekin alias ke andar woh already type ka hissa hai, toh IntPtr a, b; pointer dono par apply karta hai — * visibly repeat nahi hota.
typedef tumhare program ko slow kyun nahi karta?
Yeh compile time par hi original type tak resolve ho jaata hai; final machine code mein alias ka koi zikr nahi hota, toh runtime behaviour aur speed identical hote hain.
typedef struct node Node; Linked Lists code mein common kyun hai?
Ek node struct khud ko reference karta hai (struct node *next), aur alias har doosri line ko clean Node likhne deta hai jabki andar ki self-reference abhi-tak-aliased-nahi struct node use karti rehti hai.

Edge cases

Kya tum source file mein aane se pehle ek typedef naam use kar sakte ho?
Nahi. Ek variable ki tarah, alias sirf apni declaration ke baad exist karta hai; pehle ki lines naam nahi jaanti — yeh koi global macro nahi hai.
typedef struct node { struct node *next; } Node; ke andar, kya next field Node use kar sakta hai?
Nahi — parsing ke us point par Node abhi define nahi hua, toh self-reference ke liye tag form struct node *next zaroor use karna hoga. Alias sirf closing brace ke baad usable hota hai.
Agar do headers dono typedef int Handle; karte hain, toh dono include karna error hai?
Modern C (C11+) mein ek typedef ko same type mein redefine karna allowed aur harmless hai; use alag type mein redefine karna error hai. Safe rehne ke liye include-guards use karo.
Kya typedef array types par kaam karta hai, jaise typedef int Vec3[3];?
Haan. Phir Vec3 v; v ko int[3] ke roop mein declare karta hai. Array size alias ka hissa hai — lekin dhyan rakho: ek Vec3 parameter phir bhi pointer mein decay karega, normal C array rules ke mutabik.
Kya tum ek aisi typedef'd naam ko typedef kar sakte ho jo khud ek pointer hide karta hai, aur kya const intuitively behave karta hai?
Kar sakte ho, lekin const Handle jahan Handle hai int*, matlab hai "const pointer to int," "pointer to const int" nahi. const poore aliased type par apply hota hai, jo bahut logon ko surprise karta hai. Pointers in C dekho.
Kya typedef file scope, block scope, aur kisi struct ke andar legal hai?
File aur block scope mein: haan. Kisi struct body ke andar bhi tum ise declare kar sakte ho, lekin uski visibility enclosing scope ke rules follow karti hai — woh struct ka member nahi banta.

Recall Ek-line self-test

Upar ke har answer ko cover karo. Agar kisi bhi reveal ne tumse "oh, haan" kahlwaya — woh ek live misconception tha. Typedef ka matching section dobara padho.


Connections

  • Typedef — woh parent topic jinhe yeh traps stress-test karte hain.
  • Structures in C — tag-vs-alias namespace traps.
  • Pointers in C*-baking aur const placement traps.
  • Function Pointers — missing-* function-pointer trap.
  • Linked Lists — self-referential struct alias timing trap.