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
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
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
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.
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
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.If you need a variable across functions, use a global variable.
Quzah.
Hope is the first step on the road to disappointment.
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.And if you need a piece of information in multiple functions, pass it instead of using a global variable.