#include <iostream> using namespace std; int x=15; int main() { int x=10; { int x = 5; cout<<::x; } return 0; } is there any way to print x=10 without changing variable names and position of cout?
8/11/2020 6:46:31 AM
Padala Vamsi6 Answers
New AnswerGreat👍, now you will ask how to access private member of a class.
Yes their one way to print 10 write cout<<x; after first cout before return 0 this will print 10 #include <iostream> using namespace std; int x=15; int main() { int x=10; { int x = 5; cout<<::x; } cout<<x; return 0; }
sai vamsi Try to define more variables in main function's primary block, even better, define various types of variables in primary block. Then try again : )
#include <iostream> using namespace std; int x=15; int main() { int x=10; { int x = 5; cout<<*((&x) + 1); } return 0; } But this could print x=10 in some compilers. If warnings are avoided.
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message