1
#include "stdafx.h"
#include <iostream>
using namespace std;
int a=10;
void main()
{
 a=20;
 cout<<a<<endl;
 cout<<::a<<endl;
}
output: 20
           20

I actually want the "cout<<::a<<endl" output 10, but failed. Does "::a" mean a is in the gloable scope?
What should I have to change the code to make the second ouput 10 ? using "::a"

3
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by ~s.o.s~
0

Create a local variable called a. Otherwise your global variables won't be hidden inside main.

#include "stdafx.h"
#include <iostream>
using namespace std;
int a=10;
void main()
{
 int a=20;
 cout<<a<<endl;
 cout<<::a<<endl;
}
1
#include "stdafx.h"
#include <iostream>
using namespace std;
int a=10;
[B] void[/B] main()
{
 a=20;
 cout<<a<<endl;
 cout<<::a<<endl;
}

BTW just as a reminder, main (void) doesnt return void , it returns an int which is the signal to the operating system about the execution status of the program. A zero is returned on flawless execution while a non-zero return implies there was some problem faced while executing the code.

Hope it helped, bye.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.