Thread: local and global variables???

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    local and global variables???

    I have been doing some reading and am having trouble understanding local and global varialbes. What is the difference and when should I use each?

    Thanks is advance for the guidence.
    geo_c

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    when you declare variables within a function you can't modify their value directly within any other function but the function you declared them in. Those variable are called local variables.

    But when you let your variables declarations precede all of the functions (not in any function even 'main') you will be able to modify them directly in any funcition. Those variables are called global variables.

    Examples :

    Local Variable X :

    Code:
    #include<stdio.h>
    
    int main()
    {
    int x;
    
    scanf("%d", &x);
    printf("%d", x);
    
    return 0;
    }

    Global Variable X :

    Code:
    #include<stdio.h>
    
    int x;
    
    void function(void);
    
    int main()
    {
    x = 5;
    function();
    return 0;
    }
    
    void function()
    {
    int y;
    y = x + 10;
    printf("%d", y);
    }

    If you need a variable in more than one function then use global variables. Otherwise make it local.


    hope this make things clear
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    variables have scope and lifespan
    Global variables have a scope that is the entire program and a lifespan that is the entire program
    Local variable have a scope that is just the function they were declared
    Auto local variables (default) have a lifespan of the function in which they reside
    Static local variables have a lifespan of the entire program

    And if you need a piece of information in multiple functions, pass it instead of using a global variable.

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Local and Global variables differ essetially in two things:
    1. The space for a Local Auto Variable is allocated in the stack frame of the function in which it was defined and so it ceases to be visible once the function is executed (returns). The space for a Global variable is allocated in the 'Data' section of the object Image and hence is available for different functions and remains thoughout the lifetime of the program*.
    *,2) The visibility of a local variable is limited only to the function in which it is declared, whereas the visibility of a global variable is across all the functions defined after the variable's declaration. (Ok, both the points are related to each other anyway).
    Usage:
    If you need variables only for some purpose within a function, go for local variables (Remember that they vanish as soon as you come out of that function).
    If you need a variable across functions, use a global variable. (But be careful, because a global variable can be accessed/modified by any function, so if you are not careful, some function may modify it unexpectedly therby unplugging your program of its intended use).

    -Harsha.
    Help everyone you can

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you need a variable across functions, use a global variable.
    Or you could do what most people do, and opt to pass the variable to the function you need it in. Pass it directly if you don't need to modify it, otherwise pass a pointer to it. It's seldom a better choice to use a global variable instead of what I've mentioned.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    And if you need a piece of information in multiple functions, pass it instead of using a global variable.
    This is really a design issue but it is usually good advice Well, if this was the C++ board, std::cout, std::cin and std::clog are global variable. C's use of errno can also be a global variable, but if I remember correctly, it doesn't have to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Local vs Global Variables
    By BENCHMARKMAN in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 05:17 AM
  2. Craps Program with Local Variables
    By tigrfire in forum C Programming
    Replies: 12
    Last Post: 11-09-2005, 09:01 AM
  3. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  4. Global Vs Local Variables
    By Waldo2k2 in forum C++ Programming
    Replies: 17
    Last Post: 11-11-2002, 07:51 PM
  5. global and local variables
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2001, 01:17 PM