The output of the following C++ Program is:
#include <stdio.h>
int main (void)
{
int x, *p;
x = 30;
p = x;
print f ("%d", *p);
return 0;
}
नीचे दिए गए C++ प्रोग्राम का आउटपुट है:
#include <stdio.h>
int main (void)
{
int x, *p;
x = 30;
p = x;
print f ("%d", *p);
return 0;
}
Explanation:
Let's analyze the provided C++ program:
#include <stdio.h>
int main (void)
{
int x, *p;
x = 30;
p = x; // Error here
printf("%d", *p);
return 0;
}
In the given code:
- int x, *p; declares an integer x and a pointer to an integer p.
- x = 30; assigns the value 30 to x.
- p = x; attempts to assign the integer value x to the pointer p, which is incorrect.
- In C++, p should be assigned the address of x using p = &x;.
The line printf("%d", *p); attempts to print the value pointed to by p, but since p is not correctly assigned a valid address, this will result in an error.
Because p = x; is invalid in C++ (you cannot assign an integer directly to a pointer without casting or referencing), the program will not compile successfully and will result in a compilation error.
Thus, the correct answer is: Error
व्याख्या:
आइए दिए गए C++ प्रोग्राम का विश्लेषण करें:
#include <stdio.h>
int main (void)
{
int x, *p;
x = 30;
p = x; // यहाँ एरर है
printf("%d", *p);
return 0;
}
दिए गए कोड में:
• int x, *p; एक इंटीजर x और एक इंटीजर पॉइंटर p को डिक्लेयर करता है।
• x = 30; x को 30 वैल्यू असाइन करता है।
• p = x; पॉइंटर p को इंटीजर वैल्यू x असाइन करने की कोशिश करता है, जो गलत है।
• C++ में, p को p = &x; का इस्तेमाल करके x का एड्रेस असाइन किया जाना चाहिए।
लाइन printf("%d", *p); उस वैल्यू को प्रिंट करने की कोशिश करती है जिसे p पॉइंट करता है, लेकिन चूंकि p को सही ढंग से कोई वैलिड एड्रेस असाइन नहीं किया गया है, इसलिए इससे एरर आएगा।
क्योंकि C++ में p = x; इनवैलिड है (आप कास्टिंग या रेफरेंसिंग के बिना सीधे पॉइंटर को इंटीजर असाइन नहीं कर सकते), इसलिए प्रोग्राम सफलतापूर्वक कंपाइल नहीं होगा और कंपाइलेशन एरर देगा।
इस प्रकार, सही उत्तर है: Error
Exam Preparation Simplified
Trusted by 8.1 Crore+ Students