Global variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

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?

11th Aug 2020, 6:46 AM
Padala Vamsi
Padala Vamsi - avatar
4 Answers
+ 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; }
11th Aug 2020, 6:12 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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 : )
11th Aug 2020, 7:52 AM
Ipang
+ 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.
11th Aug 2020, 7:39 AM
Padala Vamsi
Padala Vamsi - avatar
0
Use a different variable name... Like y...
12th Aug 2020, 3:30 AM
Sanjay Kamath
Sanjay Kamath - avatar