Open In App

Output of C++ programs | Set 23

Last Updated : 29 May, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Question 1 What will be the output?




#include <iostream>
using namespace std;
  
int main()
{
    int x = 0;
    for (int i = 0; i < 10; i++)
    {
        x = x++ + ++x;
        if (x > 100)
            break;
    }
  
    cout << x;
    return 1;
}


Answer: Compiler dependent. The subexpression x++ causes a side effect, it modifies x’s value, which leads to undefined behavior since i is also referenced elsewhere in the same expression. Please refer Sequence Points in C | Set 1 for details.

Question 2 How many times does the cout executes?




#include <iostream>
using namespace std;
  
int main()
{
    int n = 10;
    for (int i = 0; i < n; i++ )
    {
        n++;
        continue;
        cout<< n;
    }
  
    return 1;
}


Answer:

No Output

The continue statement will never let the cout statement get executed and thus never executed.

Question 3 What will be the output?




#include <iostream>
using namespace std;
  
int main()
{
    int n = 10, i;
  
    for (i=0; i<n; i++)
    {
        n++;
        cout<< n << endl;
        goto x;
    }
  
x:
    do
    {
        cout << "label x"<< endl;
        break;
    }
    while( 0 ) ;
  
    return 1;
}


Answer:

11
label x

Description:
This program is executed normally by entering for loop and in the first iteration, the control jumps to label x. We need to be careful while using goto statement because it may turn the program to infinite repetitions. For example, in the above program if we keep the for loop after the label then it will turn into infinite iterations.

Question 4 What will be the output?




#include <iostream>
using namespace std;
  
int main()
{
    int choice = 1 ;
  
    switch(choice)
    {
        cout << "\nFirst Statement";
    case 1 :
        cout << "\nInside Switch case 1";
    case 2 :
        cout << "\nInside Switch case 2";
        break;
    case 3 :
        cout << "\nInside Switch case 3";
        break;
    default:
        cout << "bye bye";
    }
    return(0);
}


Answer:

Inside Switch case 1
Inside Switch case 2


Previous Article
Next Article

Similar Reads

Output of C Programs | Set 1
Predict the output of below programs. Question 1 C/C++ Code #include&lt;stdio.h&gt; int main() { int n; for(n = 7; n!=0; n--) printf(&quot;n = %d&quot;, n--); getchar(); return 0; } Output:Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2 C/C++ Code #include&lt;stdio.h&gt; int main() { pr
2 min read
Output of C Programs | Set 2
Predict the output of below programs. Question 1 C/C++ Code #include&lt;stdio.h&gt; char *getString() { char str[] = &quot;Will I be printed?&quot;; return str; } int main() { printf(&quot;%s&quot;, getString()); getchar(); } Output: Some garbage value The above program doesn't work because array variables are stored in Stack Section. So, when getS
2 min read
Output of C Programs | Set 3
Predict the output of the below program. Question 1 C/C++ Code #include &lt;stdio.h&gt; int main() { printf(&quot;%p&quot;, main); getchar(); return 0; } Output: Address of function main. Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function. Symbol table is implemented like this. st
3 min read
Output of C Programs | Set 4
Predict the output of below programs Question 1 C/C++ Code #include‹stdio.h› int main() { struct site { char name[] = &amp;quot;GeeksforGeeks&amp;quot;; int no_of_pages = 200; }; struct site *ptr; printf(&amp;quot;%d&amp;quot;,ptr-&amp;gt;no_of_pages); printf(&amp;quot;%s&amp;quot;,ptr-&amp;gt;name); getchar(); return 0; } Output: Compiler errorExp
2 min read
Output of C Programs | Set 5
Predict the output of below programs Question 1 C/C++ Code int main() { while(1){ if(printf(&quot;%d&quot;,printf(&quot;%d&quot;))) break; else continue; } return 0; } Output: Can't be predictedExplanation: The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and continue in the body of the while loop, so i
5 min read
Output of C Programs | Set 6
Predict the output of below programs Question 1 int main() { unsigned int i=65000; while ( i++ != 0 ); printf(&quot;%d&quot;,i); return 0; } Output: 1 Explanation: It should be noticed that there's a semi-colon in the body of while loop. So even though, nothing is done as part of while body, the control will come out of while only if while conditio
4 min read
Output of C Programs | Set 7
Predict the output of below programsQuestion 1 C/C++ Code int main() { int i = 0; while (i &amp;lt;= 4) { printf(&amp;quot;%d&amp;quot;, i); if (i &amp;gt; 3) goto inside_foo; i++; } getchar(); return 0; } void foo() { inside_foo: printf(&amp;quot;PP&amp;quot;); } Output: Compiler error: Label "inside_foo" used but not defined.Explanation: Scope of
2 min read
Output of C programs | Set 8
Predict the output of below C programs. Question 1: C/C++ Code #include&lt;stdio.h&gt; int main() { int x = 5, p = 10; printf(&quot;%*d&quot;, x, p); getchar(); return 0; } Output: 10 Explanation: Please see standard printf function definition int printf ( const char * format, ... ); format: String that contains the text to be written to stdout. It
3 min read
Output of C Programs | Set 9
Predict the output of the below programs. Question 1 C/C++ Code int main() { int c=5; printf(&quot;%d\n%d\n%d&quot;, c, c &lt;&lt;= 2, c &gt;&gt;= 2); getchar(); } Output: Compiler dependent The evaluation order of parameters is not defined by the C standard and is dependent on compiler implementation. It is never safe to depend on the order of par
2 min read
Output of C Programs | Set 11
Asked by Shobhit #include&lt;stdio.h&gt; int fun(int n, int *fg) { int t, f; if(n &lt;= 1) { *fg = 1; return 1; } t = fun(n-1, fg); f = t + *fg; *fg = t; return f; } int main( ) { int x = 15; printf ( &quot;%d\n&quot;, fun (5, &amp;x)); getchar(); return 0; } In the above program, there will be recursive calls till n is not smaller than or equal to
2 min read