Same variable name, one outside the function and one inside the function.


#include <iostream.h>
#include <conio.h>

int x=3;
void main()
{
      int x=2;
      cout<<x;      // x=2 will be printed


}

If I want to print "x" having value 3 in the void main() function.
What instructions should I use ? Is this possible in C or C++ ?

vicky2kAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:

cout << ::x

Not a good idea to name variables like this but it should work.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skypalaeCommented:
Hi vicky2k,

cout << ::x ; // x=3 will be printed

Cheers! S.
0
mrwad99Commented:
>> Not a good idea to name variables like this but it should work.

Yeah a better idea would be to put the variable in a namespace, then refer to the variable via full qualification.

EG

namespace myNamespace
{
   int x;
}

int main()
{
   // refer to myNamespace::x to get the x in the namespace, any other use of x will refer to a local defined in main()
}
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

vicky2kAuthor Commented:
Thanks for your comments.
0
mrwad99Commented:
You may want to close this question if there is nothing else you require assistance with...
0
Julian HansenCommented:
That was interesting!
0
mrwad99Commented:
julianH:

>> That was interesting!

Agreed.  I have requested re-open and changing of accepted answer, see
http://www.experts-exchange.com/Community_Support/Q_21220987.html.  skypalae you are free to disagree with what I said in the above link.

Cheers !
0
skypalaeCommented:
I agree. I read and answer EE for fun and I'm definitely not a point hunter. Anyway, if I can say a word to the point split, you, mrwat99, deserve more points, since your solution teaches new (and better) technique. Using global namespace can be dangerous and should be omited.

S.
0
Julian HansenCommented:
Points are a way of saying thank you. The reason for my post was that I find it interesting that both skypalae and myself gave a correct answer and were not even acknowleged for it with a post.

I agree the namespace solution is better - however, you should use namespaces when it makes sense to do so not to get around bad programming practices. In practice giving a global variable and a local variable the same name within the same module does not make a lot of sense especially from a readability perspective of using naming conventions to denote type and scope.

Just a comment on mrwad99's post

...
   // refer to myNamespace::x to get the x in the namespace, any other use of x will refer to a local defined in main()
...

This is not quite correct as x could be defined globally as well and still be used within main without a namespace reference. I am sure this is what you meant - just thought I would point it out to avoid confusion.
 
The following code gives the different scoping options

#include <iostream>

using namespace std ;

namespace mynamespace
{
      int x ;
}

int x = 3 ;

int main(int argc, char* argv[])
{
      int x = 2;

      mynamespace::x = 4 ;

      cout << x << endl ; // local variable
      cout << ::x << endl ; // global variable in default namespace
      cout << mynamespace::x << endl ; // global variable in mynamespace

      return 0;
}
Output:
2
3
4

Here is an interesting question though - is there a way of referring to both local variables x in the following implementation of main (assume the rest of the code above).

...
int main(int argc, char* argv[])
{
      int x = 2; <-- Is there a way of accessing this variable's value in the block below?

      mynamespace::x = 4 ;
      {
            int x = 5 ;

            cout << x << endl ; // local variable
            cout << ::x << endl ; // global variable in default namespace
            cout << mynamespace::x << endl ; // global variable in mynamespace
      }
      return 0;
}
0
vicky2kAuthor Commented:
How can I should give points... I am new here.
0
vicky2kAuthor Commented:
CetusMOD,
My all points for julianH.

Thanks!
0
Julian HansenCommented:
How about an even split julianH, skypalae, mrwad99 maybe a bit more for mrwad99 a) for the namespace addition and b) for taking the time to raise this in community support
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.