Sizeof Operator and Type Casting (Implicit vs Explicit) Notes in Hindi & English : sizeof ऑपरेटर और टाइप कास्टिंग (इम्प्लिसिट बनाम एक्सप्लिसिट) और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Sizeof Operator and Type Casting (Implicit vs Explicit) (sizeof ऑपरेटर और टाइप कास्टिंग (इम्प्लिसिट बनाम एक्सप्लिसिट)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: The compile-time sizeof operator computes object or type byte sizes as size_t without evaluating its operand (except for C99 variable-length arrays), while type casting governs implicit promotion and explicit value conversion. Mastering integer promotions, array-to-pointer decay, and division casting prevents classic data-loss bugs.

अध्याय सार: कंपाइल-टाइम `sizeof` ऑपरेटर अपने ऑपरेंड को चलाए बिना (C99 VLA को छोड़कर) बाइट आकार `size_t` में देता है, जबकि टाइप कास्टिंग इम्प्लिसिट प्रमोशन व एक्सप्लिसिट रूपांतरण को नियंत्रित करती है। इंटीजर प्रमोशन, एरे-टू-पॉइंटर डिके व डिवीजन कास्टिंग समझना अत्यंत आवश्यक है।

Sizeof Operator and Type Casting (Implicit vs Explicit): Detailed Notes & Architecture (sizeof ऑपरेटर और टाइप कास्टिंग (इम्प्लिसिट बनाम एक्सप्लिसिट))

1. Compile-Time sizeof Operator, size_t & VLA Runtime Exception (कंपाइल-टाइम sizeof ऑपरेटर, size_t और VLA अपवाद)

[EN] `sizeof` is a unary compile-time operator—NOT a library function—that yields the storage size in bytes (in multiples of `char`, where `sizeof(char) == 1` always) as an unsigned integer type `size_t`, printed portably using `%zu`. Parentheses are required around a type name (`sizeof(double)`), whereas around an expression they are optional (`sizeof x`). Crucially, `sizeof` inspects ONLY the static type without executing side effects, so `sizeof(x++)` leaves `x` unchanged. The sole runtime exception introduced in C99 is a Variable Length Array (VLA): `int n = 5; int a[n]; sizeof(a)` must evaluate at runtime.

[HI] `sizeof` कोई फंक्शन नहीं बल्कि एक यूनरी कंपाइल-टाइम ऑपरेटर है जो किसी डेटा टाइप या वेरिएबल का आकार बाइट्स में `size_t` प्रकार के मान के रूप में लौटाता है (इसे `%zu` से प्रिंट किया जाता है)। डेटा टाइप पर कोष्ठक अनिवार्य हैं (`sizeof(double)`), पर वेरिएबल पर वैकल्पिक (`sizeof x`) हैं। `sizeof` अपने अंदर के एक्सप्रेशन को रन नहीं करता, अतः `sizeof(x++)` से `x` का मान नहीं बदलता। इसका एकमात्र अपवाद C99 का वेरिएबल-लेंथ एरे (VLA) है जहाँ आकार रनटाइम पर निकलता है।

2. Array Decay Trap, String Literals & Struct Padding (एरे डिके ट्रैप, स्ट्रिंग लिटरल व स्ट्रक्ट पैडिंग)

[EN] Locally in the scope where `int arr[10];` is declared, `sizeof(arr)` equals `10 * sizeof(int)` (e.g., 40 bytes), allowing element count computation via `sizeof(arr) / sizeof(arr[0])`. However, when `arr` is passed to a function `void f(int arr[])`, the parameter decays to `int *`, so `sizeof(arr)` inside `f()` returns only the pointer size (4 or 8 bytes), NOT 40! Additionally, `sizeof("ABC")` returns 4 (3 characters plus trailing null `'\0'`), whereas `strlen("ABC")` returns 3. For `struct` types, `sizeof` includes internal alignment padding bytes plus trailing padding.

[HI] जहाँ `int arr[10];` घोषित हो, वहां `sizeof(arr)` पूरे एरे का आकार (जैसे 40 बाइट्स) देता है जिससे `sizeof(arr) / sizeof(arr[0])` तत्वों की गिनती निकालता है। किंतु किसी फंक्शन में पास करते समय एरे पॉइंटर में बदल (Decay) जाता है, जिससे फंक्शन के भीतर `sizeof(arr)` केवल पॉइंटर का आकार (4 या 8 बाइट्स) बताता है। इसी प्रकार `sizeof("ABC")` नल कैरेक्टर `'\0'` को गिनकर 4 देता है, जबकि `strlen` 3 लौटाता है।

3. Implicit Type Conversion: Integer Promotion & Arithmetic Hierarchy (इम्प्लिसिट टाइप रूपांतरण: इंटीजर प्रमोशन व अंकगणितीय पदानुक्रम)

[EN] Implicit type conversion (coercion) occurs automatically without programmer syntax in assignments and mixed-type expressions. Under Integer Promotion, any type smaller than `int` (`char`, `signed char`, `unsigned char`, `short`, `_Bool`) promotes automatically to `int` (or `unsigned int`) before arithmetic: thus `char a = 10, b = 20; sizeof(a + b)` equals `sizeof(int)` (4 bytes), and in C `sizeof('A')` equals `sizeof(int)` because character constants have type `int` (unlike `1` in C++!). Next, Usual Arithmetic Conversions widen operands along `int -> unsigned int -> long -> float -> double -> long double`, causing subtle bugs when signed negatives turn into large unsigned values in `-1 < 1U`.

[HI] प्रोग्रामर के लिखे बिना स्वचालित रूप से होने वाला परिवर्तन इम्प्लिसिट टाइप रूपांतरण कहलाता है। इंटीजर प्रमोशन नियम में `char` या `short` जैसे छोटे प्रकार गणना से पहले स्वतः `int` में बदल जाते हैं—इसीलिए `sizeof(a + b)` और C में `sizeof('A')` का मान `sizeof(int)` (4 बाइट्स) आता है (जबकि C++ में `sizeof('A')` 1 होता है)। विभिन्न टाइप मिलने पर छोटा टाइप बड़े टाइप में रूपांतरित होता है, जहाँ साइन्ड व अनसाइन्ड मिलने पर ऋणात्मक मान बड़ी अनसाइन्ड संख्या में बदल सकता है।

4. Explicit Type Casting (type) expr, Truncation & Safety Hazards (एक्सप्लिसिट टाइप कास्टिंग `(type) expr`, ट्रंकेशन व खतरे)

[EN] Explicit type casting forces conversion using the unary prefix syntax `(target_type) expression`, which sits at unary precedence (tier 2, right-to-left) and binds tighter than `*`, `/`, or `+`. Hence `(double)7 / 2` casts `7` first to `7.0` and divides to yield `3.5`, whereas `(double)(7 / 2)` divides integers first (`3`) and yields `3.0`. Casting floating-point values to integer types (`(int)-3.99`) truncates fractional digits toward zero (`-3`), while casting an out-of-range double (like `1e30`) to `int` causes Undefined Behavior. Never cast away `const` to mutate a truly `const` object, and beware narrowing truncation on 64-to-32 bit casts.

[HI] एक्सप्लिसिट कास्टिंग `(target_type) expression` सिंटैक्स से बलपूर्वक टाइप बदलती है और यूनरी प्राथमिकता (स्तर 2) रखती है। अतः `(double)7 / 2` में पहले 7 को `double` बनाकर `3.5` मिलता है, जबकि `(double)(7 / 2)` पहले पूर्णांक भाग `3` करके `3.0` देता है। फ्लोटिंग से `int` में कास्ट करने पर दशमलव भाग शून्य की ओर कट जाता है (`(int)-3.99 == -3`), और रेंज से बाहर के `double` को `int` में कास्ट करना Undefined Behavior है।

Syntax, Formula & Technical Example (Sizeof Operator and Type Casting (Implicit vs Explicit))

#include <stdio.h>
int main() {
    int x = 5, arr[4] = {1, 2, 3, 4};
    size_t s = sizeof(x++);
    double r1 = 7 / 2, r2 = (double)7 / 2;
    printf("sz=%zu x=%d r1=%.1f r2=%.1f len=%zu\n", s, x, r1, r2, sizeof(arr)/sizeof(arr[0]));
    return 0;
}
// Output: sz=4 x=5 r1=3.0 r2=3.5 len=4

Sizeof Operator and Type Casting (Implicit vs Explicit) Quick Reference & Comparison Table

Mechanism / ConstructStandard C RuleExam Critical Fact
Compile-Time sizeof(expr)Computes byte size as size_t without running operandsizeof(x++) leaves x value completely unchanged
C99 VLA sizeof(vla)Evaluates array dimension at runtime for VLAsSole runtime evaluation exception for sizeof
Implicit Integer PromotionWidens char and short operands automatically to intchar + char produces int result in expressions
Explicit Cast (type)exprUnary prefix cast applied before binary math(double)7 / 2 yields 3.5 instead of 3.0

Sizeof Operator and Type Casting (Implicit vs Explicit) Top 20 Bilingual MCQs with Answers

Q1. Why is `sizeof` categorized as an operator rather than a function in C?

(C में `sizeof` को फंक्शन के बजाय एक ऑपरेटर क्यों माना जाता है?)

(A) It returns a signed int value only

(B) It is declared inside <stdio.h>

(C) It executes exclusively at runtime on the heap

(D) It accepts type names like sizeof(int) and resolves at compile time without parentheses around variables

Correct Answer: (D)

[EN] No C function can accept a raw type name (`int`) as an argument or omit parentheses (`sizeof x`); sizeof is built into the compiler.

[HI] कोई भी C फंक्शन सीधे डेटा टाइप (`int`) को आर्ग्युमेंट नहीं बना सकता और न ही कोष्ठक छोड़ सकता है; `sizeof` कंपाइलर का अंतर्निहित ऑपरेटर है।

Q2. What is the exact return type of `sizeof` and its portable `printf` specifier in C99+?

(`sizeof` का सटीक रिटर्न टाइप क्या है और C99+ में इसे प्रिंट करने का मानक फॉर्मेट स्पेसिफायर कौन सा है?)

(A) unsigned int, printed with %d

(B) size_t, printed with %zu

(C) int, printed with %u

(D) long long, printed with %lld

Correct Answer: (B)

[EN] ISO C defines sizeof to yield size_t (an unsigned integer type from <stddef.h>), whose exact C99 specifier is %zu.

[HI] ISO C में `sizeof` का परिणाम `size_t` (अनसाइन्ड इंटीजर प्रकार) होता है जिसे पोर्टेबल रूप से `%zu` से प्रिंट किया जाता है।

Q3. On a 4-byte `int` machine, what does `int k = 10; printf("%zu %d", sizeof(k++), k);` output?

(4-बाइट `int` वाले सिस्टम पर `int k = 10; printf("%zu %d", sizeof(k++), k);` क्या आउटपुट देगा?)

(A) 4 11

(B) 4 10

(C) Compile error

(D) 8 10

Correct Answer: (B)

[EN] sizeof does not evaluate its non-VLA operand at runtime, so k++ never runs and k stays 10.

[HI] गैर-VLA ऑपरेंड पर `sizeof` रनटाइम मूल्यांकन नहीं करता, अतः k++ चलता ही नहीं और k का मान 10 ही रहता है।

Q4. Under ISO C99 and later, in which single case DOES `sizeof` evaluate its operand at runtime?

(ISO C99 और उसके बाद के मानकों में किस एकमात्र स्थिति में `sizeof` अपने ऑपरेंड का मूल्यांकन रनटाइम पर करता है?)

(A) When the operand is a pointer to struct

(B) When the operand is a string literal

(C) When the operand has a Variable Length Array (VLA) type

(D) When the operand contains ++ or --

Correct Answer: (C)

[EN] For a VLA such as `int a[n];`, the size depends on runtime variable `n`, forcing runtime evaluation.

[HI] C99 वेरिएबल-लेंथ एरे (`int a[n];`) का आकार रनटाइम वेरिएबल `n` पर निर्भर होने से `sizeof` को रनटाइम पर गणना करनी पड़ती है।

Q5. On a 64-bit platform (`sizeof(int) == 4`, `sizeof(int *) == 8`), what does `void fn(int a[]) { printf("%zu", sizeof(a)); }` print when passed `int arr[10];`?

(64-बिट सिस्टम (`sizeof(int)==4`, `sizeof(int*)==8`) पर `int arr[10]` को `void fn(int a[])` में भेजने पर `sizeof(a)` क्या प्रिंट करेगा?)

(A) 10

(B) 40

(C) 4

(D) 8

Correct Answer: (D)

[EN] Array parameters in C functions decay to pointers (`int *a`), so sizeof(a) measures the pointer size (8 bytes), not the array's 40 bytes.

[HI] फंक्शन पैरामीटर में एरे `int *a` पॉइंटर में डिके हो जाता है, इसलिए फंक्शन के अंदर `sizeof(a)` केवल पॉइंटर का आकार (8 बाइट) बताता है।

Q6. Assuming 1-byte `char` and 4-byte `int`, what are `sizeof('A')` in C and `sizeof('A')` in C++ respectively?

(1-बाइट `char` व 4-बाइट `int` मानकर C तथा C++ में क्रमशः `sizeof('A')` के मान क्या होते हैं?)

(A) 4 in both C and C++

(B) 1 in both C and C++

(C) 1 in C and 4 in C++

(D) 4 in C and 1 in C++

Correct Answer: (D)

[EN] In C, character literals like `'A'` have type `int` (4 bytes), whereas C++ defines character literals as type `char` (1 byte).

[HI] C भाषा में कैरेक्टर कॉन्स्टेंट `'A'` का टाइप `int` (4 बाइट) होता है, जबकि C++ में इसका टाइप `char` (1 बाइट) होता है।

Q7. What does `printf("%zu", sizeof("Hello"));` output in C?

(C में `printf("%zu", sizeof("Hello"));` क्या आउटपुट देता है?)

(A) 8

(B) 6

(C) 5

(D) 4

Correct Answer: (B)

[EN] String literal "Hello" is an array of 6 chars (`H`,`e`,`l`,`l`,`o`,`\0`), so sizeof includes the null terminator and returns 6.

[HI] स्ट्रिंग लिटरल "Hello" में 5 अक्षरों के साथ अंत का नल बाइट `'\0'` भी शामिल होता है, अतः `sizeof` 6 लौटाता है।

Q8. Given `char a = 10, b = 20;` on a 4-byte `int` system, what is `sizeof(a + b)`?

(4-बाइट `int` सिस्टम पर `char a = 10, b = 20;` के लिए `sizeof(a + b)` कितना होगा?)

(A) 1

(B) 4

(C) 2

(D) 8

Correct Answer: (B)

[EN] Integer promotion converts both char operands to int before `+` executes, so `a + b` has type `int` (size 4).

[HI] `+` होने से पहले इंटीजर प्रमोशन दोनों `char` को `int` में बदल देता है, इसलिए `a + b` का आकार 4 बाइट आता है।

Q9. Compare `(float)9 / 2` and `(float)(9 / 2)` in C. What values do they yield?

(C में `(float)9 / 2` और `(float)(9 / 2)` के मान क्रमशः क्या होते हैं?)

(A) 4.0 and 4.0

(B) 4.0 and 4.5

(C) 4.5 and 4.0

(D) 4.5 and 4.5

Correct Answer: (C)

[EN] In `(float)9 / 2`, unary cast binds tighter than `/`, promoting 9 to 9.0f for float division (4.5); `(float)(9 / 2)` runs integer 9/2 = 4 first, yielding 4.0.

[HI] `(float)9 / 2` में पहले 9 को float बनाकर भाग देने से 4.5 मिलता है, जबकि `(float)(9 / 2)` में पहले पूर्णांक भाग 4 होकर 4.0 मिलता है।

Q10. What does `printf("%d %d", (int)4.85, (int)-4.85);` output in C?

(C में `printf("%d %d", (int)4.85, (int)-4.85);` क्या प्रिंट करेगा?)

(A) 5 -4

(B) 5 -5

(C) 4 -5

(D) 4 -4

Correct Answer: (D)

[EN] Explicit floating-to-integer conversion in C always truncates the fractional part toward zero (never rounds).

[HI] C में फ्लोटिंग से पूर्णांक में कास्ट करने पर दशमलव भाग हमेशा शून्य की ओर ट्रंकेट (कट) होता है, राउंड नहीं होता।

Q11. What happens in `int x = 1000000; double d = 1e35; x = (int)d;` in ISO C?

(ISO C में `double d = 1e35;` को `(int)d` द्वारा `int` में बदलने पर क्या होता है?)

(A) Undefined Behavior because 1e35 exceeds int's representable range

(B) Raises a compile-time overflow error

(C) Wraps cleanly modulo 2^32

(D) Safely clamps x to INT_MAX

Correct Answer: (A)

[EN] Converting a floating-point value to an integer type when the truncated integer does not fit in the target range is Undefined Behavior in ISO C.

[HI] रेंज से बाहर की फ्लोटिंग संख्या को `int` में कास्ट करने पर ISO C के अनुसार Undefined Behavior होता है।

Q12. Where are parentheses MANDATORY when using the `sizeof` operator in C?

(C में `sizeof` ऑपरेटर के साथ कोष्ठक `()` लगाना कब अनिवार्य होता है?)

(A) When the operand is a type name such as sizeof(int)

(B) When the operand is a pointer variable

(C) When the operand is a local int variable

(D) Parentheses are optional in all cases

Correct Answer: (A)

[EN] C grammar defines `sizeof ( type-name )` with required parentheses and `sizeof unary-expression` without required parentheses.

[HI] डेटा टाइप (जैसे `sizeof(int)`) के साथ कोष्ठक लगाना अनिवार्य है, जबकि वेरिएबल एक्सप्रेशन (`sizeof x`) पर यह वैकल्पिक होता है।

Q13. If `char c = 120;` (`signed char`, max +127), what happens in `char res = (c * 2) / 3;`?

(यदि `signed char c = 120;` हो, तो `char res = (c * 2) / 3;` में `res` का मान क्या होगा?)

(A) -5

(B) Overflow wrap-around to negative number

(C) 80 (no overflow during intermediate calculation due to integer promotion)

(D) Compile error on char multiplication

Correct Answer: (C)

[EN] c promotes to int before multiplication: 120 * 2 = 240 fits easily in int, then 240 / 3 = 80 fits back cleanly into signed char.

[HI] गुणा से पहले `c` इंटीजर प्रमोशन से `int` बन जाता है: `120 * 2 = 240` बिना ओवरफ्लो हुए 3 से भाग होकर `80` बनता है जो `char` में फिट हो जाता है।

Q14. Which expression accurately calculates the element count of a locally declared fixed array `double vals[12];`?

(स्थानीय रूप से घोषित `double vals[12];` एरे में तत्वों की कुल संख्या निकालने का सटीक सूत्र कौन सा है?)

(A) sizeof(vals) / sizeof(vals[0])

(B) sizeof(vals) / sizeof(int)

(C) strlen(vals)

(D) sizeof(*vals) / sizeof(vals)

Correct Answer: (A)

[EN] Dividing total array byte size by the byte size of one element (`vals[0]`) yields 12 cleanly regardless of element type.

[HI] एरे के कुल आकार को उसके एक तत्व के आकार (`sizeof(vals[0])`) से भाग देने पर किसी भी डेटा टाइप के तत्वों की सटीक संख्या मिल जाती है।

Q15. Which statement is NOT true about `sizeof` and type casting in C?

(C में `sizeof` और टाइप कास्टिंग के बारे में इनमें से कौन सा कथन सत्य नहीं है?)

(A) sizeof(char) is guaranteed by ISO C to be 1

(B) sizeof(arr) inside a function receiving `int arr[]` returns the original caller array byte count

(C) Implicit conversion widens narrower arithmetic operands to match wider ones

(D) Type cast operator (type) has higher precedence than binary *, /, and +

Correct Answer: (B)

[EN] Inside a function, an array parameter `int arr[]` is adjusted to `int *`, so `sizeof(arr)` returns pointer size, losing the original array length.

[HI] फंक्शन के भीतर एरे पैरामीटर `int *` पॉइंटर बन जाता है, इसलिए वहां `sizeof(arr)` मूल एरे का आकार नहीं बल्कि पॉइंटर का आकार लौटाता है।

Q16. What is the output of `unsigned int u = 1; int s = -5; printf("%d", (s < u));` on a 32-bit compiler?

(32-बिट कंपाइलर पर `unsigned int u = 1; int s = -5; printf("%d", (s < u));` का आउटपुट क्या होगा?)

(A) 1

(B) 0

(C) -5

(D) Compile error

Correct Answer: (B)

[EN] Implicit conversion widens signed int s (-5) to unsigned int (UINT_MAX - 4), and UINT_MAX - 4 < 1U is false (0).

[HI] इम्प्लिसिट रूपांतरण साइन्ड `-5` को बहुत बड़े अनसाइन्ड पूर्णांक (`UINT_MAX - 4`) में बदल देता है, जो `1U` से छोटा नहीं है, अतः 0 प्रिंट होता है।

Q17. Within Sizeof Operator and Type Casting (Implicit vs Explicit), which mechanism / construct directly maps to: "Computes byte size as size_t without running operand"?

(Sizeof Operator and Type Casting (Implicit vs Explicit) में कौन सा Mechanism / Construct सीधे "Computes byte size as size_t without running operand" से संबंधित है?)

(A) Implicit Integer Promotion

(B) Explicit Cast (type)expr

(C) Compile-Time sizeof(expr)

(D) C99 VLA sizeof(vla)

Correct Answer: (C)

[EN] In Sizeof Operator and Type Casting (Implicit vs Explicit), 'Compile-Time sizeof(expr)' handles 'Computes byte size as size_t without running operand' (sizeof(x++) leaves x value completely unchanged).

[HI] Sizeof Operator and Type Casting (Implicit vs Explicit) में 'Compile-Time sizeof(expr)' का कार्य/विवरण 'Computes byte size as size_t without running operand' (sizeof(x++) leaves x value completely unchanged) है।

Q18. Which option accurately describes 'C99 VLA sizeof(vla)' in Sizeof Operator and Type Casting (Implicit vs Explicit)?

(Sizeof Operator and Type Casting (Implicit vs Explicit) के संदर्भ में 'C99 VLA sizeof(vla)' की सही विशेषता कौन सी है?)

(A) Unary prefix cast applied before binary math

(B) Computes byte size as size_t without running operand

(C) Widens char and short operands automatically to int

(D) Evaluates array dimension at runtime for VLAs

Correct Answer: (D)

[EN] 'C99 VLA sizeof(vla)' provides 'Evaluates array dimension at runtime for VLAs' — key point: Sole runtime evaluation exception for sizeof.

[HI] 'C99 VLA sizeof(vla)' का मुख्य विवरण 'Evaluates array dimension at runtime for VLAs' है (Sole runtime evaluation exception for sizeof)।

Q19. In Sizeof Operator and Type Casting (Implicit vs Explicit), identify the correct parameter for the specification: "Widens char and short operands automatically to int".

(Sizeof Operator and Type Casting (Implicit vs Explicit) में "Widens char and short operands automatically to int" विवरण किस विकल्प के लिए सही है?)

(A) Compile-Time sizeof(expr)

(B) C99 VLA sizeof(vla)

(C) Implicit Integer Promotion

(D) Explicit Cast (type)expr

Correct Answer: (C)

[EN] 'Implicit Integer Promotion' corresponds to 'Widens char and short operands automatically to int' (char + char produces int result in expressions).

[HI] 'Implicit Integer Promotion' का संबंध 'Widens char and short operands automatically to int' (char + char produces int result in expressions) से है।

Q20. Considering Sizeof Operator and Type Casting (Implicit vs Explicit), what key behavior defines 'Explicit Cast (type)expr'?

(Sizeof Operator and Type Casting (Implicit vs Explicit) में 'Explicit Cast (type)expr' की मुख्य भूमिका या मान क्या है?)

(A) Computes byte size as size_t without running operand

(B) Evaluates array dimension at runtime for VLAs

(C) Widens char and short operands automatically to int

(D) Unary prefix cast applied before binary math

Correct Answer: (D)

[EN] 'Explicit Cast (type)expr' represents 'Unary prefix cast applied before binary math' ((double)7 / 2 yields 3.5 instead of 3.0).

[HI] 'Explicit Cast (type)expr' का अर्थ/मान 'Unary prefix cast applied before binary math' है ((double)7 / 2 yields 3.5 instead of 3.0)।

Sizeof Operator and Type Casting (Implicit vs Explicit) — Exam FAQs

Q1: Why can't `sizeof` find the length of a dynamically allocated array (`int *p = malloc(10 * sizeof(int))`)?

`malloc` से बनाए गए डायनामिक एरे (`int *p`) की लंबाई `sizeof(p)` से क्यों नहीं निकल सकती?

[EN] `p` has static type `int *` (a pointer), and `sizeof` resolves static types at compile time. Thus `sizeof(p)` always returns pointer size (4 or 8 bytes), never the allocated runtime block size—you must store `n` separately.

[HI] `p` एक `int *` पॉइंटर है और `sizeof` कंपाइल-टाइम पर केवल पॉइंटर वेरिएबल का आकार (4 या 8 बाइट) मापता है, न कि हीप मेमोरी की लंबाई। इसलिए एरे का आकार अलग वेरिएबल में सहेजना पड़ता है।

Q2: What is the key difference between implicit type conversion and explicit type casting?

इम्प्लिसिट टाइप रूपांतरण और एक्सप्लिसिट टाइप कास्टिंग में मुख्य अंतर क्या है?

[EN] Implicit conversion (coercion) is performed automatically by the compiler following Integer Promotion and Usual Arithmetic Conversion rules, whereas explicit casting uses `(type)expr` syntax written by the programmer to force a specific conversion (like `(double)a / b`).

[HI] इम्प्लिसिट रूपांतरण कंपाइलर द्वारा नियम अनुसार स्वतः किया जाता है, जबकि एक्सप्लिसिट कास्टिंग में प्रोग्रामर `(type)expr` सिंटैक्स लिखकर जानबूझकर डेटा टाइप बदलता है।

Q3: Why is `sizeof('a')` equal to 4 in C while it is 1 in C++?

C में `sizeof('a')` का मान 4 जबकि C++ में 1 क्यों होता है?

[EN] In C, character constants such as `'a'` are defined as type `int`, so `sizeof('a') == sizeof(int)`. In C++, character literals have type `char` (to support function overloading cleanly), giving `sizeof('a') == 1`.

[HI] C में `'a'` जैसे कैरेक्टर कॉन्स्टेंट का टाइप `int` होता है (अतः साइज 4), जबकि C++ में फंक्शन ओवरलोडिंग की सुविधा के लिए इसका टाइप `char` (साइज 1) रखा गया है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)