C++ Reference Variable| Find output programs | Set 2

This section contains the C++ find output programs with their explanations on C++ Reference Variable (set 2).
Submitted by Nidhi, on June 09, 2020

Program 1:

#include <iostream>
using namespace std;

int A = 10;

int& fun()
{
    return A;
}

int main()
{
    fun() = 20;

    cout << A;

    return 0;
}

Output:

20

Explanation:

In the above program, we created a global variable A with initial value 10. In C++, the function that returns a reference of a variable, that can be used as an LVALUE. So that here we can set a value into the global variable.

Here, we set the value 20 into global variable A using function fun().

Program 2:

#include <iostream>
using namespace std;

void fun(int& X, int& Y)
{
    X = X - EOF - NULL;
    Y = Y - (-EOF) + NULL;
}

int main()
{

    int A = 10;
    int B = 20;

    fun(A, B);

    cout << A << " " << B;

    return 0;
}

Output:

11 19

Explanation:

  • The value of NULL is 0.
  • The value of EOF is -1.

Here we declared two variables A and B with initial values 10 and 20 respectively. And, we created function fun() that takes an argument as a reference. So, that changes done in the reference variable will be reflected in A and B

Now, evaluate the expressions,

X = X - EOF    - NULL;
Y = Y - (-EOF) + NULL;

Expression 1:
	X = 10 - -1  - 0
	X = 10 + 1 -0
	X = 11

Expression 2:
	Y = Y - (-EOF) + NULL
	Y = 20 – (- -1) + 0
	Y = 20 – 1 + 0
	Y = 19

Then the final result will be: 11 19






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.