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;
}
Discussion:
20 comments Page 2 of 2.
Pompy said:
1 decade ago
Answer is D as Y is storing the address of X, so --Y will result in 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>.
Vinay ramani said:
10 years ago
Declaration syntax error.
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.
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.
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;.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers