Question bank — Typedef
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.
typedef int Age; Age a = 5; int b = a; bina kisi cast ke compile hota hai.
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.
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.
* 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.
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.
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.
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.
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.
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?
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?
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?
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.
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?
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?
typedef struct Point Point; baad mein struct keyword drop karne deta hai kyun?
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?
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?
* 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?
typedef struct node Node; Linked Lists code mein common kyun 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?
typedef struct node { struct node *next; } Node; ke andar, kya next field Node use kar sakta hai?
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?
Kya typedef array types par kaam karta hai, jaise typedef int Vec3[3];?
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?
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?
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 aurconstplacement traps. - Function Pointers — missing-
*function-pointer trap. - Linked Lists — self-referential struct alias timing trap.