Skip to content
Related Articles
Open in App
Not now

Related Articles

Output of C++ programs | Set 22

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 16 May, 2017
Improve Article
Save Article

Predict the output of the following C++ programs.

Question 1




#include <iostream>
using namespace std;
  
int main()
{
    int a = b = c = 0;
    cout << a << "*" << b << "*" << c;
    return 0;
}

Output:

Compile time error!

Explanation:
A chained statement cannot be used to initialize variables at the time of declaration. Hence the statement a = b = c = 0; is an illegal statement. However following way a legal syntax and can be used in C++ or C.

int a,b,c;
      a = b = c = 0;

Question 2




#include <iostream>
using namespace std;
  
int main()
{
    for ( ; ; ) cout << "blank";
    return 0;
}

Output:

Infinite Loop!

Explanation:
Since the initialization, test condition and increment/decrement condition is missing inside for loop the execution will be caught in infinite loop.

Question 3




#include <iostream>
using namespace std;
  
int main()
{  
   int i;
   for (i=0; i<3; i++);
        
   cout << "hello!" <<i;
  
   return 0;
  
}

Output:

hello!3

Explanation:
Hello!3 is the result because when for loop executes it does not have any task to do due to the semi-colon(;) present in the line where for loop has been defined. The loop will execute three times and the value of i will become 3 then rest of the statement(s) will execute.

Question 4




#include <iostream>
using namespace std;
  
int main()
{
    int i;
    i = 1 + (1,4,5,6,3);
    cout << i;
    return 0;
}

Output:

4

Explanation:
The comma (,) operator is a binary operator that evaluates the first operand and discards the result and then evaluates the second and then returns the value of second. Here the associativity of the comma (,) operator is from left to right and is easy to understand that the expression(1,4,5,6,3) evaluates to be 3 and then the result 1 + 3 is assigned to i.

Question 5




#include <iostream>
using namespace std;
  
int main()
{
    int a = 0, b;
    b = (a = 50) + 10;
    cout << a << "$" << b;
    return 0;
}

Output:

50$60

Explanation:
The statement b = (a = 50) + 10; uses the concept of embedded assignment. Here, the value of 50 is assigned to variable a and the result 50+10 is assigned to b.

Question 6




#include<iostream>
using namespace std;
  
int main()
{
    char a = 30, b = 40, c = 10;
    char d = (a*b)/c;
    cout << int(d);
  
    return 0;
}

Output:

120

Explanation:
The C++ can also perform arithmetic calculation considering the ASCII values of characters if the arithmetic operation are performed on variables of char datatype. Here in this case answer is 120 which is the ASCII value for x.

Question 7




#include<iostream>
using namespace std;
  
int main(int x)
{
    static int i = 5;
  
    if (--i)
    {
        cout << i;
        main(10);
    }
    return 0;
}

Output:

4321

Explanation:
Any non-zero number in C++ is treated as true value. Here in this code the if statement tends to decrease the value of i but inside the block of if, the main() function is called again and again. Here the program seems to be in infinite loop but the variable i is static in nature it has its lifetime till execution hence the program will halt if i becomes 0.

Question 8




#include<iostream>
using namespace std;
  
int main(int x)
{
    int i = 5;
  
    if (--i)
    {
        cout << i;
        main(10);
    }
    return 0;
}

Output:

infinite loop

Explanation:
Any non-zero number in C++ is treated as true value. Here in this code the if statement tends to decrease the value of i but inside the block of if, the main() function is called again and again (all the statements will now execute as if it is a new program) hence the program execution will be caught in infinite loop as there is no termination condition.

This article is contributed by Avinash Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!