Thread: Local vs Global Variables

  1. #1
    Registered User
    Join Date
    Apr 2006

    Local vs Global Variables

    Quick question. If I have a global variable and a local variable with the same name and I use the variable which is used? Does the local variable take precedence or the global variable. Is 50 or 100 printed from the example below?

    int x = 50;
    void foo()
      int x = 100;

  2. #2
    Registered User
    Join Date
    Sep 2006
    You get the local version by default. You can access the global version of x by using ::x instead of x (the :: is called the scope resolution operator).

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    In general, this should never occur. Your variables should have names such that a conflict between a local and a global variable cannot occur.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > Is 50 or 100 printed from the example below?
    Did you try it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2004
    They really shouldn't have the same name, as was mentioned earlier, but if you wanted to find out do what Salem said ^^ there.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User
    Join Date
    Jul 2007
    One thing though, as standard practice, you should never ever use a global variable. It tends to create the potential for your program's stability to be compromised. Attributes that belong to a class should always exist as private attributes, and you should only ever be able to access them via methods and friend functions.

    Although about which comes first.. Its all about scope. Which ever has the most recent scope (or the smaller scope I guess) Global has the scope of the entire class, next is method scope, then you can have if/while/for scopes. If you redefine it in the smallest end of the scope (which is the if/while/for), it'll take precedence over method and global. Hope that makes sense :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  2. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  3. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  4. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 10:53 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM