CUET PG 2021 — Computer PYQ
CUET PG | Computer | 2021What is the output of this C code?
#include<stdio.h>
Void main()
{
Int x = 18;
void *ptr = &x;
printf(“%f/n”, *(foat)ptr);
}
Choose the correct answer:
- A.
compile time error
- B.
undefined behaviour
(Correct Answer) - C.
10
- D.
0.000000
undefined behaviour
Explanation
Solution
Iska result Compiler Error ya Undefined Behavior/Garbage Value hoga.
-
Syntax Errors:
-
Void,Int,foat—C language case-sensitive hai, isliye lowercase (void,int,float) hona chahiye. -
printfmeinfoatki spelling galat hai aurptrko cast karte waqt*miss ho sakta hai.
-
-
Logical Error (Type Punning):
Agar hum maan lein ki syntax sahi hai:
*(float*)ptr, toh bhi result galat aayega.-
Yahan x ek integer hai (18).
-
Pointer ptr us integer ka address store kar raha hai.
-
Jab aap
(float*)ptrkarte hain, toh compiler us memory address ko ek float ki tarah read karne ki koshish karta hai.
-
Mathematical & Memory Reason
Integer aur Float ka memory representation bilkul alag hota hai.
-
Integer (18): Binary mein yeh kuch aisa dikhta hai (32-bit):
00000000 00000000 00000000 00010010 -
Float Representation: C mein floats IEEE 754 standard follow karte hain:
Value=(−1)s×M×2E−127
Jab compiler integer 18 ke binary pattern ko float ke formula mein dalta hai, toh value 0.000000 ya koi bahut hi ajeeb Garbage Value aati hai, kyunki exponent bits zero hote hain.

