Open In App

Output of C++ programs | Set 50

Last Updated : 07 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of the following C++ programs:
 

  • Question 1
     

CPP




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


  • Output: 
     
1804289383
  • Explanation: As the declared number is an integer, It will produce the random number from 0 to RAND_MAX. The value of RAND_MAX is library-dependent but is guaranteed to be at least 32767 on any standard library implementation.
  • Question 2:
     

CPP




#include <iostream>
 
using namespace std;
 
int array1[] = { 1200, 200, 2300, 1230, 1543 };
int array2[] = { 12, 14, 16, 18, 20 };
int temp, result = 0;
int main()
{
    for (temp = 0; temp < 5; temp++) {
        result += array1[temp];
    }
    for (temp = 0; temp < 5; temp++) {
        result += array2[temp];
    }
    cout << result;
    return 0;
}


  • Output: 
     
2147483647
  • Explanation: The output is Compiler Dependent. RAND_MAX is a function used by the compiler to create a maximum random number.
  • Question 3:
     

CPP




#include <iostream>
using namespace std;
int main()
{
    int a = 5, b = 10, c = 15;
    int arr[3] = { &a, &b, &c };
    cout << *arr[*arr[1] - 8];
    return 0;
}


  • Output: 
     
Compile time error
  • Explanation: void will not accept any values to its type. 
     
  • Question 4:
     

CPP




#include <iostream>
using namespace std;
int main()
{
    int array[] = { 10, 20, 30 };
    cout << -2 [array];
    return 0;
}


  • Output: 
     
6553
  • Explanation: In this program we are adding the every element of two arrays. All the elements of array1[] and array2[] will be added and the sum will be stored in result and hence output is 6553.
  • Question 5:
     

CPP




#include <iostream>
using namespace std;
int main()
{
    int const p = 5;
    cout << ++p;
    return 0;
}


  • Output: 
     
Compile time error!
  • Explanation: The conversion is invalid in this array. The array arr[] is declared to hold integer type value but we are trying to assign references(addresses) to the array so it will arise error. The following compilation error will be raised: 
     
cannot convert from ‘int *’ to ‘int’
  • Question 6:
     

CPP




#include <iostream>
using namespace std;
int main()
{
    char arr[20];
    int i;
    for (i = 0; i < 10; i++)
        *(arr + i) = 65 + i;
    *(arr + i) = '\0';
    cout << arr;
    return (0);
}


  • Output: 
     
-30
  • Explanation: -2[array]: this statement is equivalent to -(array[2]). At the zero index 30 is stored hence negation of 30 will be printed due to unary operator (-).
  • Question 7:
     

CPP




#include <iostream>
using namespace std;
int Add(int X, int Y, int Z)
{
    return X + Y;
}
double Add(double X, double Y, double Z)
{
    return X + Y;
}
int main()
{
    cout << Add(5, 6);
    cout << Add(5.5, 6.6);
    return 0;
}


  • Output: 
     
Compile time Error!
  • Explanation: Constant variables are those whose value can’t be changed throughout the execution. ++p statement try to change the value hence compiler will raise an error.
  • Question 8:
     

CPP




#include <iostream>
using namespace std;
#define PR(id) cout << "The value of " #id " is " << id
int main()
{
    int i = 10;
    PR(i);
    return 0;
}


  • Output: 
     
ABCDEFGHIJ
  • Explanation: Each time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned. So it will print from A to J. 
     
  • Question 9:
     

CPP





  • Output: 
     
Compile time error!
  • Explanation: Here we want to add two element but in the given functions we take 3 arguments.So compiler doesn’t get the required function(function with 2 arguments)
  • Question 10:
     

CPP




#include <iostream>
using namespace std;
#define PR(id) cout << "The value of " #id " is " << id
int main()
{
    int i = 10;
    PR(i);
    return 0;
}


  • Output: 
     
The value of i is 10
  • Explanation: In this program, we are just printing the declared value through a macro. Carefully observe that in macro there is no semicolon(;) used as a termination statement.

sing.inbox@gmail.com.

 



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
Article Tags :