Global variables | SoloLearn: Learn to code for FREE!

+3

Global variables

#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?

c++ c

8/11/2020 6:46:31 AM

sai vamsi

6 Answers

New Answer

+4

No way of doing that

+6

Great👍, now you will ask how to access private member of a class.

+6

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; }

+3

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 : )

+1

#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.

0

Use a different variable name... Like y...