What will be the output of the program? void main() { char* p; printf("%d %d",…
2024
What will be the output of the program?
void main()
{
char* p;
printf("%d %d", sizeof(*p), sizeof(p));
}
Answer: D. The answer is compiler specific — The sizeof() operator returns the number of bytes occupied by its operand. In the given code, p is a character pointer. The expression *p dereferences the…
- A.
1 1
- B.
1 8
- C.
2 1
- D.
The answer is compiler specific
Attempted by 347 students.
Show answer & explanation
Correct answer: D
The sizeof() operator returns the number of bytes occupied by its operand. In the given code, p is a character pointer. The expression *p dereferences the pointer, so it refers to a char. Therefore, sizeof(*p) evaluates to 1 byte, as a char occupies 1 byte.
The expression sizeof(p) returns the size of the pointer variable itself, not the data it points to. The size of a pointer depends on the system architecture: 2 bytes on 16-bit systems, 4 bytes on 32-bit systems, and 8 bytes on 64-bit systems. Since the size of the pointer varies across different compilers and platforms, the output of the program is not fixed.
Thus, the output depends on the compiler and the system architecture. The correct answer is that the output is compiler-specific.
हिन्दी उत्तर: sizeof() ऑपरेटर अपने ऑपरेंड द्वारा लिए गए बाइट्स की संख्या देता है। यहाँ p एक चर इंटीरर पॉइंटर है। *p ऑपरेटर पॉइंटर को डीरेफरेंस करता है, इसलिए यह एक चर को संदर्भित करता है। इसलिए, sizeof(*p) 1 बाइट का मान देता है, क्योंकि एक चर 1 बाइट लेता है।
पॉइंटर p का sizeof(p) पॉइंटर वेरिएबल के आकार को देता है, न कि वह डेटा जिसे यह संदर्भित करता है। पॉइंटर का आकार सिस्टम आर्किटेक्चर पर निर्भर करता है: 16-बिट सिस्टम पर 2 बाइट, 32-बिट सिस्टम पर 4 बाइट और 64-बिट सिस्टम पर 8 बाइट। चूँकि पॉइंटर का आकार विभिन्न कंपाइलर और प्लेटफॉर्म पर भिन्न होता है, प्रोग्राम का आउटपुट निश्चित नहीं होता है।
इसलिए, आउटपुट कंपाइलर और सिस्टम आर्किटेक्चर पर निर्भर करता है। सही उत्तर यह है कि आउटपुट कंपाइलर-विशिष्ट है।