Typedef in C: Creating Custom Type Aliases Notes in Hindi & English : typedef कीवर्ड: डेटा प्रकारों को उपनाम देना और कोड सरलीकरण और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Typedef in C: Creating Custom Type Aliases (typedef कीवर्ड: डेटा प्रकारों को उपनाम देना और कोड सरलीकरण) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The typedef keyword is a compiler-level storage-class specifier that creates readable aliases for existing types—simplifying structs, pointers, and function-pointer signatures. Unlike #define text macros, typedef obeys C scoping rules and binds pointer qualifiers uniformly across multi-variable declarations like iptr a, b;.

अध्याय सार: typedef कीवर्ड कंपाइलर स्तर पर मौजूदा डेटा प्रकारों के लिए सरल उपनाम (Alias) बनाता है, जिससे struct, पॉइंटर व फंक्शन पॉइंटर के जटिल सिंटैक्स आसान हो जाते हैं। #define मैक्रो के विपरीत typedef स्कोप नियमों का पालन करता है और iptr a, b; में दोनों वेरिएबल्स को सुरक्षित पॉइंटर बनाता है।

Typedef in C: Creating Custom Type Aliases: Detailed Notes & Architecture (typedef कीवर्ड: डेटा प्रकारों को उपनाम देना और कोड सरलीकरण)

1. typedef Semantics, Storage-Class Slot & Standard Aliases (typedef कार्यप्रणाली, स्टोरेज-क्लास नियम व मानक उपनाम)

[EN] typedef introduces a synonym (alias) for an existing data type—it neither allocates runtime memory nor invents an incompatible type. Syntax mirrors variable declaration with typedef in front: typedef unsigned long ulong; makes ulong interchangeable with unsigned long. Standard library types such as size_t, ptrdiff_t, int32_t, and FILE are all typedef aliases ensuring cross-platform portability. Because typedef occupies the storage-class specifier slot in C grammar, combining typedef with static or extern causes a syntax error.

[HI] typedef किसी पहले से मौजूद डेटा टाइप को केवल एक नया उपनाम देता है; यह न तो अतिरिक्त मेमोरी लेता है और न ही कोई भिन्न असंगत टाइप बनाता है। मानक लाइब्रेरी के size_t, ptrdiff_t, uint32_t और FILE सभी typedef उपनाम हैं। व्याकरण की दृष्टि से यह स्टोरेज-क्लास स्लॉट में आता है, इसलिए typedef static int x; लिखना अवैध है।

2. Simplifying struct, union, enum & Self-Referential Nodes (struct, union व सेल्फ-रेफरेंशियल नोड सरलीकरण)

[EN] In pure C (unlike C++), using a struct tag requires repeating the struct keyword on every declaration (struct Student s1;). Wrapping it via typedef struct Student { int id; } Student; allows clean declarations like Student s1;. Beware a classic linked-list exam rule: inside typedef struct Node { int data; ??? next; } Node;, writing Node *next; fails because the alias Node isn't in scope until after the closing semicolon—you MUST write struct Node *next;.

[HI] शुद्ध C में हर बार struct Student s1; लिखने के बजाय typedef struct Student { int id; } Student; लिखकर सीधे Student s1; उपयोग किया जा सकता है। किंतु लिंक्ड लिस्ट बनाते समय अंदर पॉइंटर में Node *next; लिखने पर त्रुटि आएगी क्योंकि Node उपनाम सेमीकोलन के बाद सक्रिय होता है; इसलिए अंदर struct Node *next; लिखना अनिवार्य है।

3. Taming Complex Array & Function Pointer Declarators (जटिल एरे व फंक्शन पॉइंटर सिंटैक्स को सरल बनाना)

[EN] Complex declarators quickly become unreadable without typedef. Defining typedef int (*MathOp)(int, int); creates a crisp callback type alias MathOp so you can declare dispatch arrays like MathOp ops[4]; rather than cryptic int (*ops[4])(int, int);. Likewise, typedef int Row5[5]; lets Row5 grid[3]; represent a 3x5 matrix, and typedef char *String; improves parameter clarity.

[HI] जटिल पॉइंटर घोषणाओं को पढ़ने योग्य बनाने में typedef बेहद कारगर है। typedef int (*MathOp)(int, int); द्वारा फंक्शन पॉइंटर का उपनाम MathOp बनाकर कॉलबैक व एरे MathOp ops[4]; आसानी से लिखे जा सकते हैं। इसी प्रकार typedef int Row5[5]; से Row5 mat[3]; जैसा 2D एरे बनाना सहज हो जाता है।

4. Critical Distinction: typedef vs #define Macros (मुख्य अंतर: typedef बनाम #define प्रीप्रोसेसर मैक्रो)

[EN] Although both can alias simple types, typedef is evaluated by the compiler (respecting block scope), whereas #define performs blind string replacement during preprocessing. This difference exposes a famous C pointer trap: with typedef int* iptr; iptr a, b;, BOTH a and b are int * pointers. In contrast, #define IPTR int* followed by IPTR a, b; expands literally to int* a, b;, making a an int * pointer while b silently becomes a plain int! Also note const iptr p; makes p a constant pointer (int * const p), not pointer-to-const.

[HI] typedef कंपाइलर द्वारा संभाला जाता है और लोकल ब्लॉक स्कोप मानता है, जबकि #define प्रीप्रोसेसर द्वारा मात्र टेक्स्ट बदलाव करता है। इसका प्रसिद्ध अंतर पॉइंटर में दिखता है: typedef int* iptr; के बाद iptr a, b; में a और b दोनों int * पॉइंटर बनते हैं; पर #define IPTR int* के बाद IPTR a, b; का विस्तार int* a, b; होकर केवल a को पॉइंटर और b को साधारण int बना देता है!

Syntax, Formula & Technical Example (Typedef in C: Creating Custom Type Aliases)

#include <stdio.h>
typedef int* IntPtr;
typedef int (*MathFn)(int, int);
typedef struct Node { int data; struct Node *next; } Node;
int add(int a, int b) { return a + b; }
int main(void) {
    int x = 5, y = 9; IntPtr p1 = &x, p2 = &y; MathFn fn = add;
    printf("%d %d\n", *p1 + *p2, fn(3, 4)); // 14 7
    return 0;
}

Typedef in C: Creating Custom Type Aliases Quick Reference & Comparison Table

Alias ConstructCompiler / Preprocessor EffectExam Crucial Distinction
typedef int* IntPtr;Compiler binds pointer type to both a, b in IntPtr a, bAvoids multi-variable pointer trap of #define
#define IPTR int*Preprocessor expands IPTR a, b to int* a, bOnly a becomes int *; b degrades to plain int
typedef struct N {...} N;Creates alias N usable without struct keywordSelf-pointer inside still requires struct N *next
typedef int (*Fn)(int);Creates readable function-pointer alias FnSimplifies callback parameters and jump tables

Typedef in C: Creating Custom Type Aliases Top 20 Bilingual MCQs with Answers

Q1. What is the primary purpose of the typedef keyword in C?

(C भाषा में typedef कीवर्ड का मुख्य उद्देश्य क्या है?)

(A) Allocating dynamic heap memory automatically

(B) Replacing preprocessor #include directives

(C) Defining a completely new incompatible runtime class

(D) Creating an alternative name (alias) for an existing data type

Correct Answer: (D)

[EN] typedef gives an existing type a clearer or shorter alias without creating a distinct incompatible type.

[HI] typedef किसी मौजूदा डेटा टाइप को एक नया, सरल या अर्थपूर्ण उपनाम (Alias) प्रदान करता है।

Q2. Which translation phase processes typedef versus #define?

(typedef और #define को क्रमशः कंपाइलेशन के किस चरण द्वारा संभाला जाता है?)

(A) The preprocessor handles both typedef and #define

(B) The compiler processes typedef; the preprocessor processes #define

(C) The linker processes typedef while the compiler handles #define

(D) The preprocessor processes typedef; the compiler handles #define

Correct Answer: (B)

[EN] #define performs textual substitution before compilation, whereas typedef is parsed semantically by the compiler.

[HI] #define को प्रीप्रोसेसर टेक्स्ट के रूप में बदलता है, जबकि typedef को कंपाइलर डेटा टाइप के रूप में समझता है।

Q3. Given typedef char* cptr; cptr p1, p2;, what are the exact data types of p1 and p2?

(typedef char* cptr; के बाद cptr p1, p2; घोषित करने पर p1 और p2 के डेटा टाइप क्या होंगे?)

(A) p1 is char * and p2 is plain char

(B) Compile-time error

(C) Both p1 and p2 are char * pointers

(D) Both p1 and p2 are plain char

Correct Answer: (C)

[EN] Because cptr is a true compiler type alias for char *, every variable listed in cptr p1, p2; receives type char *.

[HI] चूंकि cptr कंपाइलर का वास्तविक टाइप उपनाम है, इसलिए p1 और p2 दोनों char * पॉइंटर बनेंगे।

Q4. Contrast: Given #define CPTR char* followed by CPTR p1, p2;, what are the types of p1 and p2?

(#define CPTR char* के बाद CPTR p1, p2; घोषित करने पर p1 और p2 के डेटा टाइप क्या होंगे?)

(A) Both p1 and p2 are char * pointers

(B) p1 is char and p2 is char *

(C) p1 is char *, whereas p2 is a plain char variable

(D) Both p1 and p2 are plain char

Correct Answer: (C)

[EN] The preprocessor literally substitutes char* p1, p2;, where the asterisk binds only to p1.

[HI] प्रीप्रोसेसर इसे सीधे char* p1, p2; में बदलता है, जिसमें * केवल p1 पर लागू होता है और p2 साधारण char रह जाता है।

Q5. Why does typedef static int sint; fail to compile in C?

(C में typedef static int sint; लिखने पर कंपाइल त्रुटि क्यों आती है?)

(A) In C grammar, typedef is itself a storage-class specifier and cannot be combined with static/extern/auto

(B) typedef must come after static

(C) static integers cannot be aliased in C

(D) sint is a reserved C keyword

Correct Answer: (A)

[EN] C permits at most one storage-class specifier per declaration, and typedef occupies that slot.

[HI] C व्याकरण में typedef स्वयं एक स्टोरेज-क्लास स्पेसिफायर है, अतः एक साथ static या extern नहीं लगाया जा सकता।

Q6. In typedef struct Node { int val; ??? next; } Node;, what MUST replace ??? inside the struct body?

(typedef struct Node { int val; ??? next; } Node; में संरचना के भीतर ??? की जगह क्या लिखना अनिवार्य है?)

(A) struct Node *

(B) typedef Node *

(C) Node *

(D) void

Correct Answer: (A)

[EN] The typedef name Node is not recognized until after the closing semicolon, so self-referential fields must use struct Node *.

[HI] उपनाम Node अभी परिभाषित हो रहा है और सेमीकोलन के बाद मान्य होता है, इसलिए अंदर struct Node * लिखना अनिवार्य है।

Q7. How much extra runtime memory does declaring a typedef alias consume?

(एक typedef उपनाम घोषित करने से रनटाइम पर कितनी अतिरिक्त मेमोरी खर्च होती है?)

(A) 0 bytes (compile-time alias only)

(B) Equal to sizeof(original_type)

(C) 4 bytes on 32-bit, 8 bytes on 64-bit

(D) 1 byte symbol overhead in RAM

Correct Answer: (A)

[EN] typedef is purely a compile-time naming abstraction and reserves zero memory by itself.

[HI] typedef केवल कंपाइल-टाइम नामकरण नियम है; यह रनटाइम में कोई मेमोरी नहीं घेरता।

Q8. If typedef unsigned int uint; is written inside a function body { ... }, what is its visibility scope?

(यदि typedef unsigned int uint; को किसी फंक्शन ब्लॉक { ... } के भीतर लिखा जाए, तो उसका स्कोप क्या होगा?)

(A) Global across all linked .c files

(B) Invalid—typedef cannot appear inside functions

(C) Entire translation unit automatically

(D) Local block scope (visible only inside that block, unlike global #define)

Correct Answer: (D)

[EN] Unlike preprocessor #define macros (which ignore block braces), typedef obeys normal C block/file lexical scoping rules.

[HI] #define के विपरीत typedef C के सामान्य स्कोप नियमों का पालन करता है, अतः ब्लॉक के अंदर लिखा उपनाम वहीं तक सीमित रहता है।

Q9. Which declaration defines Arr10 as a typedef alias for an array of 10 integers so Arr10 a; declares int a[10];?

(कौन सी घोषणा Arr10 को 10 इंटीजर के एरे का typedef उपनाम बनाती है ताकि Arr10 a; लिखने पर int a[10]; बने?)

(A) #define Arr10[10] int

(B) typedef int Arr10[10];

(C) typedef int[10] Arr10;

(D) typedef Arr10 int[10];

Correct Answer: (B)

[EN] Following standard C declaration placement, the array brackets [10] follow the alias name: typedef int Arr10[10];.

[HI] सामान्य घोषणा की तरह नाम के बाद [10] लगाकर typedef int Arr10[10]; लिखा जाता है।

Q10. Which typedef correctly defines FuncPtr as a pointer to a function taking (int, float) and returning double?

(कौन सा typedef उस फंक्शन पॉइंटर का सही उपनाम FuncPtr बनाता है जो (int, float) लेकर double लौटाता है?)

(A) typedef double *FuncPtr(int, float);

(B) typedef FuncPtr (*double)(int, float);

(C) typedef (double)(*FuncPtr)(int, float);

(D) typedef double (*FuncPtr)(int, float);

Correct Answer: (D)

[EN] Parentheses around (*FuncPtr) distinguish a function pointer from a function returning double *.

[HI] (*FuncPtr) पर कोष्ठक आवश्यक है ताकि यह double * लौटाने वाले फंक्शन के बजाय फंक्शन पॉइंटर बने।

Q11. Given typedef int* IntPtr; const IntPtr p = &x;, what is the exact effective type of p?

(typedef int* IntPtr; के बाद const IntPtr p = &x; लिखने पर p का वास्तविक प्रभावी टाइप क्या होता है?)

(A) int * const p (a constant pointer whose address cannot change, NOT const int *p)

(B) const int *p (pointer to constant int)

(C) Compile-time error

(D) int *p (non-const pointer)

Correct Answer: (A)

[EN] const modifies the alias IntPtr as a whole (which is pointer), freezing p's stored address while allowing *p = 9;.

[HI] यहाँ const पूरे पॉइंटर टाइप IntPtr पर लगता है, जिससे p स्थिर पॉइंटर (int * const p) बनता है, न कि const int *p।

Q12. Why do <stdint.h> and <stddef.h> define types like uint32_t and size_t using typedef?

(<stdint.h> और <stddef.h> में uint32_t और size_t जैसे प्रकार typedef से क्यों बनाए जाते हैं?)

(A) To double arithmetic execution speed

(B) To store integers on the Heap automatically

(C) To guarantee portable fixed-width or architecture-appropriate types across diverse CPUs and compilers

(D) Because C forbids using unsigned int directly

Correct Answer: (C)

[EN] Library headers adjust typedef mappings per target architecture so application code remains portable.

[HI] अलग-अलग हार्डवेयर व कंपाइलर पर बिट-चौड़ाई एक जैसी व पोर्टेबल रखने के लिए हेडर फाइलों में typedef उपयोग होता है।

Q13. Is it valid in C to combine typedef with an unnamed (anonymous) struct: typedef struct { int x, y; } Point;?

(क्या C में बिना टैग वाले स्ट्रक्चर के साथ typedef जोड़कर typedef struct { int x, y; } Point; लिखना वैध है?)

(A) Only valid in C++, rejected by ISO C

(B) No, every struct MUST have a tag name before {

(C) Yes, it cleanly defines Point without needing a struct tag (unless self-referential)

(D) Valid only if x and y are chars

Correct Answer: (C)

[EN] Anonymous struct + typedef is a standard C idiom whenever self-referential pointers aren't needed inside the struct.

[HI] हाँ, यदि अंदर सेल्फ-रेफरेंशियल पॉइंटर न हो तो बिना टैग के typedef struct { ... } Point; पूर्णतः वैध है।

Q14. Trace the output: typedef unsigned char byte; byte b = 255; b++; printf("%d", b);

(आउटपुट बताएं: typedef unsigned char byte; byte b = 255; b++; printf("%d", b);)

(A) 0

(B) 255

(C) 256

(D) -1

Correct Answer: (A)

[EN] byte is an 8-bit unsigned char (range 0..255); incrementing 255 wraps modulo 256 to 0.

[HI] byte असल में unsigned char (सीमा 0..255) है, अतः 255 में +1 करने पर रैप-अराउंड होकर मान 0 हो जाता है।

Q15. Which statement is NOT true about typedef in C?

(C में typedef के बारे में इनमें से कौन सा कथन सत्य नहीं है?)

(A) typedef cannot be combined with static in the same declaration

(B) typedef obeys C scope rules inside functions or files

(C) typedef handles multi-variable pointer declarations safer than #define

(D) typedef creates a strongly distinct type that causes compile errors if mixed with its original base type

Correct Answer: (D)

[EN] In C, typedef creates only a synonym—values of the alias and original type mix freely with zero type error.

[HI] typedef एक मजबूत अलग टाइप नहीं बल्कि केवल एक उपनाम बनाता है, इसलिए यह मूल टाइप के साथ पूरी तरह घुल-मिल जाता है।

Q16. Why can typedef define array types like typedef int Vec[5]; Vec v; whereas #define cannot do #define VEC int[5]; VEC v;?

(typedef int Vec[5]; Vec v; क्यों काम करता है जबकि #define VEC int[5]; VEC v; असफल क्यों हो जाता है?)

(A) Arrays cannot be declared inside main()

(B) C syntax places [5] after the identifier v, which preprocessor substitution cannot inject around v

(C) Preprocessor macros cannot contain the int keyword

(D) #define does not allow digits like 5 in macros

Correct Answer: (B)

[EN] Expanding VEC v yields invalid syntax int[5] v;, whereas compiler typedef naturally applies the array declarator to v.

[HI] प्रीप्रोसेसर विस्तार से int[5] v; जैसा गलत सिंटैक्स बन जाता है, जबकि कंपाइलर का typedef सही ढंग से [5] को v पर लागू करता है।

Q17. Within Typedef in C: Creating Custom Type Aliases, which alias construct directly maps to: "Compiler binds pointer type to both a, b in IntPtr a, b"?

(Typedef in C: Creating Custom Type Aliases में कौन सा Alias Construct सीधे "Compiler binds pointer type to both a, b in IntPtr a, b" से संबंधित है?)

(A) typedef int* IntPtr;

(B) typedef int (*Fn)(int);

(C) typedef struct N {...} N;

(D) #define IPTR int*

Correct Answer: (A)

[EN] In Typedef in C: Creating Custom Type Aliases, 'typedef int* IntPtr;' handles 'Compiler binds pointer type to both a, b in IntPtr a, b' (Avoids multi-variable pointer trap of #define).

[HI] Typedef in C: Creating Custom Type Aliases में 'typedef int* IntPtr;' का कार्य/विवरण 'Compiler binds pointer type to both a, b in IntPtr a, b' (Avoids multi-variable pointer trap of #define) है।

Q18. Which option accurately describes '#define IPTR int*' in Typedef in C: Creating Custom Type Aliases?

(Typedef in C: Creating Custom Type Aliases के संदर्भ में '#define IPTR int*' की सही विशेषता कौन सी है?)

(A) Preprocessor expands IPTR a, b to int* a, b

(B) Compiler binds pointer type to both a, b in IntPtr a, b

(C) Creates readable function-pointer alias Fn

(D) Creates alias N usable without struct keyword

Correct Answer: (A)

[EN] '#define IPTR int*' provides 'Preprocessor expands IPTR a, b to int* a, b' — key point: Only a becomes int *; b degrades to plain int.

[HI] '#define IPTR int*' का मुख्य विवरण 'Preprocessor expands IPTR a, b to int* a, b' है (Only a becomes int *; b degrades to plain int)।

Q19. In Typedef in C: Creating Custom Type Aliases, identify the correct parameter for the specification: "Creates alias N usable without struct keyword".

(Typedef in C: Creating Custom Type Aliases में "Creates alias N usable without struct keyword" विवरण किस विकल्प के लिए सही है?)

(A) typedef int* IntPtr;

(B) #define IPTR int*

(C) typedef int (*Fn)(int);

(D) typedef struct N {...} N;

Correct Answer: (D)

[EN] 'typedef struct N {...} N;' corresponds to 'Creates alias N usable without struct keyword' (Self-pointer inside still requires struct N *next).

[HI] 'typedef struct N {...} N;' का संबंध 'Creates alias N usable without struct keyword' (Self-pointer inside still requires struct N *next) से है।

Q20. Considering Typedef in C: Creating Custom Type Aliases, what key behavior defines 'typedef int (*Fn)(int);'?

(Typedef in C: Creating Custom Type Aliases में 'typedef int (*Fn)(int);' की मुख्य भूमिका या मान क्या है?)

(A) Creates readable function-pointer alias Fn

(B) Preprocessor expands IPTR a, b to int* a, b

(C) Creates alias N usable without struct keyword

(D) Compiler binds pointer type to both a, b in IntPtr a, b

Correct Answer: (A)

[EN] 'typedef int (*Fn)(int);' represents 'Creates readable function-pointer alias Fn' (Simplifies callback parameters and jump tables).

[HI] 'typedef int (*Fn)(int);' का अर्थ/मान 'Creates readable function-pointer alias Fn' है (Simplifies callback parameters and jump tables)।

Typedef in C: Creating Custom Type Aliases — Exam FAQs

Q1: Should I use typedef or #define to name data types in C?

C में डेटा टाइप्स को नया नाम देने के लिए typedef उपयोग करें या #define?

[EN] Always prefer typedef for naming data types. It respects C block scoping, accurately propagates pointers across multiple variables (Ptr a, b;), and supports arrays and function pointers cleanly.

[HI] डेटा टाइप्स के उपनाम के लिए हमेशा typedef चुनें क्योंकि यह स्कोप नियमों का पालन करता है, एक साथ घोषित कई पॉइंटर्स (Ptr a, b;) को सही रखता है और फंक्शन पॉइंटर में भी सटीक कार्य करता है।

Q2: Can I redeclare or redefine a typedef in the same scope in C?

क्या एक ही स्कोप में typedef को दोबारा परिभाषित किया जा सकता है?

[EN] Prior to C11, redeclaring even the identical typedef in the same scope was an error; C11 explicitly allows redeclaring a typedef ONLY if it resolves to the exact same type, whereas redefining it to a different type is always an error.

[HI] C11 से पहले एक ही स्कोप में समान typedef दोबारा लिखना भी त्रुटि थी; C11 में समान टाइप के लिए पुनर्घोषणा की अनुमति है पर भिन्न टाइप से दोबारा परिभाषित करना हमेशा त्रुटि देता है।

Q3: Why do many C style guides discourage hiding pointers behind typedef like typedef struct Node *NodePtr;?

कई C कोडिंग गाइड पॉइंटर्स को typedef (जैसे NodePtr) के पीछे छिपाने से क्यों मना करती हैं?

[EN] Hiding the asterisk * makes readers forget a variable is a pointer—leading to confusion with const NodePtr p (which makes the pointer const, not the struct) and unexpected memory mutations.

[HI] * छिप जाने से प्रोग्रामर को स्पष्ट नहीं दिखता कि वेरिएबल एक पॉइंटर है; साथ ही const NodePtr p लिखने पर डेटा के बजाय स्वयं पॉइंटर const हो जाता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)