C++ Programming - References - Discussion

Discussion Forum : 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;
}
The program will print the output 80 80.
The program will print the output 81 80.
The program will print the output 81 81.
It will result in a compile time error.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
20 comments Page 2 of 2.

Vinay ramani said:   10 years ago
Declaration syntax error.

Arsen said:   10 years ago
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>.

Rumzi said:   1 decade ago
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.

Mohammad Mohsin seed said:   1 decade ago
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.

Sumi said:   1 decade ago
It should be y=&x;.

Atul Kumar said:   1 decade ago
Because the declaration of reference variable is wrong.

It should be as int &y = x;.

Sujeeth said:   1 decade ago
Declaration of y should be int& y = x; instead of int y& = x.

Vishal Gour said:   1 decade ago
Address is only stored by a pointer.

Gabgame said:   1 decade ago
Actually position of '&' is incorrect. Otherwise using Visual studio 2010, it gives 80 80 output.

Pompy said:   1 decade ago
Answer is D as Y is storing the address of X, so --Y will result in error.


Post your comments here:

Your comments will be displayed after verification.