What does the following code output?
int a = 3, b = 3;
cout << (++a == b++);
नीचे दिए गए कोड का आउटपुट क्या है?
int a = 3, b = 3;
cout << (++a == b++);
Key Points
- The code snippet involves pre-increment and post-increment operations on two integer variables a and b.
- Initially, both a and b are set to 3.
- The expression (++a == b++) is evaluated as follows:
- ++a increments a by 1 before the comparison, so a becomes 4.
- b++ increments b by 1 after the comparison, so b remains 3 during the comparison.
- Thus, the comparison (4 == 3) evaluates to false (which is represented as 0 in C++).
मुख्य बातें
• इस कोड स्निपेट में दो इंटीजर वेरिएबल a और b पर प्री-इंक्रीमेंट और पोस्ट-इंक्रीमेंट ऑपरेशन शामिल हैं।
• शुरुआत में, a और b दोनों की वैल्यू 3 सेट की गई है।
• एक्सप्रेशन (++a == b++) का मूल्यांकन इस प्रकार किया जाता है:
o ++a तुलना से पहले a की वैल्यू 1 बढ़ा देता है, इसलिए a की वैल्यू 4 हो जाती है।
o b++ तुलना के बाद b की वैल्यू 1 बढ़ाता है, इसलिए तुलना के दौरान b की वैल्यू 3 ही रहती है।
o इस प्रकार, तुलना (4 == 3) का परिणाम 'फॉल्स' (गलत) होता है (जिसे C++ में 0 के रूप में दिखाया जाता है)।
Exam Preparation Simplified
Trusted by 8.1 Crore+ Students