C++ Operators | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on various operators (set 1).
Submitted by Nidhi, on June 01, 2020

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;
}

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.

Program 2:

#include <iostream>
using namespace std;

int main()
{
    int X = 0;

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

    cout << X;
    
    return 0;
}

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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.