Open In App

Output of C++ programs | Set 21

Improve
Improve
Like Article
Like
Save
Share
Report

What will be the output of this program?




#include<iostream>
using namespace std;
int x; // Global x
  
int main()
{
    int x = 10; // Local x
    cout << "Value of global x is " << ::x << endl;
    cout << "Value of local x is " << x; 
    return 0;
}


Output:

Value of global x is 0
Value of local x is 10

In C++, global variable (if we have a local variable with same name), can be accessed using scope resolution operator (::).

 
What will be the output of this program?




#include <iostream>
using namespace std;
int a = 90;
  
int fun(int x, int *y = &a)
{
    *y = x + *y;
    return x + *y;
}
  
int main()
{
    int a = 5, b = 10;
  
    a = fun(a);
    cout << a << " " << b << endl;
  
    b = fun(::a,&a);
    cout << a << " " << b << endl;
  
    return 0;
}


100   10
195   290

There are two variables with name ‘a’, one is global and other is local. When we call a = fun(a);, it calls int fun(int x, int *y=&a), here pointer to global variable (which is a = 90) is assigned to y. Therefore.
*y = x + *y; // becomes 5 + 90
return x + *y; // becomes 5 + 95

 
What will be the output of this program?




#include <iostream>
using namespace std;
int a = 2;
  
int fun(int *a)
{
    ::a *= *a;
    cout << ::a << endl;
    return *a;
}
  
int main()
{
    int a = 9;
    int &x = ::a;
  
    ::a += fun(&x);
    cout << x;
}


Output:

4
8

The global variable is being accessed by the function fun every time using ::a. The local variable value does’nt impact on the value of a.

 
What will be the output of this program?




#include <iostream>
using namespace std;
  
int main()
{
    char *A[] = { "abcx", "dbba", "cccc"};
    char var = *(A+1) - *A+1;
    cout << (*A + var);
}


Output:

 bba

Here the array representation is A[0] = “abcx”, A[1] = “dbba”, A[2] = “cccc”. Precedence of (pointer)* >(binary) +, and order of execution of ‘*’ is right to left . If ‘A’ address is ‘x’ then address of ‘*(A+1)’ is ‘x+6’ and address of ‘*A+1’ is ‘x+1’. So integer value of var = 6 (total no of character between two points (x+6)-(x+1)+1). During printing the operator ‘+’ is overloaded now the pointer points to ‘x+7’ . For this reason the output of the program.
 
What will be the output of this program?




#include <iostream>
using namespace std;
  
int main()
{
    char a = 'a', b = 'x';
    char c = (b ^ a >> 1 * 2) +(b && a >> 1 * 2 );
    cout << " c = " << c;
}


Output:

 c = 97

Integer value of a = 97 (01100001), integer value of b = 120 (01111000), precedence of ‘*’ > ‘>>’ > ‘^’ > ‘&&’.
So expression is ‘ ((b ^ (a >> (1 * 2))) – (b && (a >> (1 * 2 ))))’. Integer Value of expression
a >> 1 * 2 = 24 b ^ a >> 1 * 2 = 96 b && a >> 1 * 2 = 1 which is 97.

 

What will be the output of this program?




#include <iostream>
using namespace std;
  
int main()
{
    int i = 5, j = 3;
    switch(j)
    {
    case 1:
        if (i < 10) cout << "\ncase 1";
        else if (i > 10)
           case 2:
               cout << "case 2";
        else if (i==10)
           case 3:
    default:
        cout << "case 3";
        cout << "hello";
    }
}


Output:

case 3hello

Since j=3 satisfies the condition, it goes in case 3. There is nothing in case 3, also there is no break. So default is executed. Please refer switch statement in C for details.



Last Updated : 07 May, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads