What happens if the following program is executed in C and C++? #include…
2023
What happens if the following program is executed in C and C++? #include <stdio.h> int main(void) { int new = 5; printf("%d", new); }
- A.
Error in C and successful execution in C++
- B.
Error in both C and C++
- C.
Error in C++ and successful execution in C
- D.
More than one of the above
- E.
None of the above
Attempted by 107 students.
Show answer & explanation
Correct answer: C
In C, the keyword 'new' is not reserved, so it can be used as a variable name. Therefore, the program compiles and runs successfully, printing 5.
In C++, 'new' is a reserved keyword used for dynamic memory allocation. Using it as a variable name results in a compilation error.
Thus, the program fails in C++ but succeeds in C.
हिन्दी उत्तर:
C में 'new' एक आरक्षित शब्द नहीं है, इसलिए इसे चर के नाम के रूप में उपयोग किया जा सकता है। इसलिए, प्रोग्राम सफलतापूर्वक कंपाइल होता है और 5 प्रिंट करता है।
C++ में, 'new' डायनामिक मेमोरी आवंटन के लिए एक आरक्षित शब्द है। इसे चर के नाम के रूप में उपयोग करने पर कंपाइलेशन त्रुटि होती है।
इसलिए, प्रोग्राम C++ में विफल होता है लेकिन C में सफल होता है।