C++ Programming - References - Discussion

Discussion :: References - Programs (Q.No.1)

1. 

Which of the following statement is correct about the program given below?

#include<iostream.h> 
int main()
{
    int x = 80; 
    int y& = x;
    x++;
    cout << x << " " << --y;
    return 0;
}

[A]. The program will print the output 80 80.
[B]. The program will print the output 81 80.
[C]. The program will print the output 81 81.
[D]. It will result in a compile time error.

Answer: Option D

Explanation:

No answer description available for this question.

Pompy said: (May 21, 2012)  
Answer is D as Y is storing the address of X, so --Y will result in error.

Gabgame said: (Sep 4, 2012)  
Actually position of '&' is incorrect. Otherwise using Visual studio 2010, it gives 80 80 output.

Vishal Gour said: (Sep 27, 2012)  
Address is only stored by a pointer.

Sujeeth said: (Aug 25, 2013)  
Declaration of y should be int& y = x; instead of int y& = x.

Atul Kumar said: (Nov 10, 2013)  
Because the declaration of reference variable is wrong.

It should be as int &y = x;.

Sumi said: (Jul 23, 2014)  
It should be y=&x;.

Mohammad Mohsin Seed said: (Jun 20, 2015)  
Actually, reference (&) need to be before why otherwise compiler gives following error in DEV CPP.

1) Expected initializer before '&' token.
2) 'y' was not declared in this scope.

Rumzi said: (Jun 23, 2015)  
Considering int &y=x;

Hey, when we do x++ the value of x should be printed as 81 and hence --y would become 80 right?

But when I ran the program in Visual Studio 13 it printed 80 80.

Please enlighten me.

Arsen said: (Oct 14, 2015)  
The main issue here is that definition of 'y' is incorrect. The '&' should be written before the variable name. Also there are several other issues: missing the namespace 'std' before 'cout' and also some compiler will generate an error regarding the 'iostream.h' included file. It is more preferable to write #include <iostream>.

Vinay Ramani said: (Oct 20, 2015)  
Declaration syntax error.

Kevin said: (Jan 31, 2016)  
Position of '&' is incorrect.

Saurabh Sharma said: (Mar 9, 2016)  
It should be y=&x ;
OR
y=y & x;
Here & is BITWISE AND.

#include<iostream.h>
#include<conio.h>r
int main()
{
int x = 80,y=10;
clrscr();
y = y & x;
x++;
cout << x << " " << --y;
getch();
return 0;
}

output.
--------
    64 32 16 8 4 2 1
x = 1 0 1 0 0 0 0
y = 0 0 0 1 0 1 0
--------------------------
y = 0 0 0 0 0 0 0

--y = -1.

Mohammad Taha said: (Oct 15, 2016)  
int y& is basically a reference variable, and reference variable is internally a constant pointer. And one cannot change constant pointer value. So, it's compile time error.

Vipul said: (Jan 10, 2018)  
cout << x << " " << --y.

This is processed right to left, starting with loading y, decrementing it and so on.

Hatos said: (Feb 27, 2018)  
I got this error:

expected initializer before '&' token
int y& = x;
^
Code::Blocks
Release 17.12 rev 11256
g++.exe (GCC) 7.2.0
std = c++1z

Can anyone help me to resolve it?

Sarvesh Karan said: (Jul 5, 2018)  
Why it is compile time error. It should take as bitwise & and then proceed.

R@J said: (Jul 18, 2018)  
#include <stdio.h>

int main()
{
int x=80;
int &y = x;
x++;
printf("Hello World %d %d ",x, --y);
return 0;
}

And the answer is 80 80.

Anurag said: (Oct 1, 2020)  
Please note that &y is just creating a reference and reference is automatically deferred so --y is purely a valid operation.

So the answer is 81 80.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.