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 1 of 2.
SAURABH SHARMA said:
9 years ago
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.
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.
Tanuja Ankush Gawade said:
1 year ago
#include<iostream.h>
int main()
{
int x = 80;
int y& = x;
x++;
cout << x << " " << --y;
return 0;
}
In the above program "using namespace std; "line or "std" library must be included and The "&" symbol must be placed before the variable name like int &y =x;
Hence there is an error.
int main()
{
int x = 80;
int y& = x;
x++;
cout << x << " " << --y;
return 0;
}
In the above program "using namespace std; "line or "std" library must be included and The "&" symbol must be placed before the variable name like int &y =x;
Hence there is an 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>.
Tauseef Sarwar said:
2 years ago
#include<iostream.h>
int main()
{
int x = 80;
int y& = x;
x++;
cout << x << " " << --y;
return 0;
}
@All.
This shows error, but when the ampersand symbol is written before y then the answer will be 81 80.
int main()
{
int x = 80;
int y& = x;
x++;
cout << x << " " << --y;
return 0;
}
@All.
This shows error, but when the ampersand symbol is written before y then the answer will be 81 80.
(1)
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.
Hatos said:
8 years ago
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?
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?
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.
Mohammad Taha said:
9 years ago
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.
R@j said:
7 years ago
#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.
int main()
{
int x=80;
int &y = x;
x++;
printf("Hello World %d %d ",x, --y);
return 0;
}
And the answer is 80 80.
(2)
Anurag said:
5 years ago
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.
So the answer is 81 80.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers