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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.