• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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

0
vicky2k
Asked:
vicky2k
  • 4
  • 3
  • 3
  • +1
3 Solutions
 
Julian HansenCommented:

cout << ::x

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

0
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now