Q:

C++ Operators | Find output programs | Set 1

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on various operators (set 1).

Program 1:

#include <iostream>
using namespace std;

int a = 10;

int main()
{
    int a = 20;
    int b = 5, c = 0;

    c = a++ + 5 * (::a)*20 / 30 - b;

    cout << c;
    return 0;
}

Program 2:

#include <iostream>
using namespace std;

int main()
{
    int X = 0;

    X = sizeof('A') * sizeof(sizeof(20.5));

    cout << X;
    
    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

48

Explanation:

The above code will print 48 on the console screen, here we use one global variable a that contains values 10, and three local variables a, b, c that contains values 20, 5, and 0 respectively.

Here we used a scope resolution operator to access the global variable a because a is also a local variable.

To understand the output we will look at expression step by step, here priority and associability table will be followed for evaluation of the expression.

    c = a+++ 5 * (::a) * 20/30-b;
    c = 20 + 5 * 10 * 20/30-5
       = 20 + 50 * 20/30-5
       = 20 + 1000/30-5
       = 20 + 33-5
       = 53 - 5
       = 48  
    
    Finally, the output will be 48.

Answer Program 2:

Output:

4 [on 32 bit compiler]
or
8 [on 64 bit compiler]

Explanation:

In the above code we used sizeof operator, the sizeof operator is used to determine the size of the specified type or specified value.

Now we understand the evaluation of expression step by step:

    X = sizeof('A') * sizeof(sizeof(20.5))

The sizeof('A') is 1 because the size of a character is 1 in C++. (Note: The sizeof('A') will be 4 in C language)

And sizeof(20.5) will be 8, (Because the size of a double number is 8 in C++).

Thus,

    For 32 bit compiler:
    X = 1 * sizeof(sizeof(20.5))
    X = 1 * sizeof(8)
    X = 1 * 4
    X = 4
    The final value of X is 4.

    For 64 bit compiler:
    X = 1 * sizeof(sizeof(20.5))
    X = 1 * sizeof(8)
    X = 1 * 8
    X = 8
    The final value of X is 8.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Operators | Find output programs | Set 2... >>