This section contains the C++ find output programs with their explanations on conditional statements (set 2).
Program 1:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int num = 0;
num = printf("%d ", printf("%d ", printf("ABC")));
if (num == 2) {
cout << "INDIA";
}
else if (num == 3) {
cout << "AUSTRALIA";
}
else {
cout << "CHINA";
}
return 0;
}
Program 2:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int num = 0;
if (EOF) {
cout << "INDIA";
}
else if (NULL) {
cout << "AUSTRALIA";
}
else {
cout << "USA";
}
return 0;
}
Answer Program 1:
Output:
Explanation:
The above program will print "ABC3 2 INDIA" on the console screen.
The printf() function returns the total number of characters printed on the console screen.
Execution of the program step by step:
num = printf("%d ",printf("%d ",printf("ABC")));
In this statement the innermost printf() will be executed first and prints "ABC" and returns 3, then the second printf() prints "3 " and returns 2 because 3 and space are two characters, and then the last outermost printf() will be executed and prints "2 " and returns 2 that is assigned to num variable.
The num variable contains 2 then the first condition will be true and it will print "INDIA".
Then the final output will be "ABC3 2 INDIA".
Answer Program 2:
Output:
Explanation:
The above program will print "INDIA" on the console screen. Because the value of the EOF macro is -1. And any non-zero value is considered as a true for if conditions, this, the first condition will be true and it will print "INDIA" on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer