🌐
W3Schools
w3schools.com › c › c_scope.php
C Variable Scope
If you operate with the same variable name inside and outside of a function, C will treat them as two separate variables; One available in the global scope (outside the function) and one available in the local scope (inside the function):
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › local-and-global-variables
Local and Global Variables - GeeksforGeeks
March 21, 2024 - Lifetime Limited to Scope: Local ... storage is required. Global variables are variables that are declared outside of any function or block of code and can be accessed from any part of the program....
People also ask

What is global variable in C?
A global variable in C is declared outside all functions and can be accessed throughout the entire program.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
Where are Global Variables Stored in C?
Global variables in C are stored in the data segment of memory and remain available throughout the program’s execution.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
Can global variables be constant in C?
Yes, you can declare a global variable as const to prevent modification, like const int MAX_LIMIT = 100;.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
🌐
Code Quoi
codequoi.com › en › local-global-static-variables-in-c
Local, Global and Static Variables in C - codequoi
This is why when main later reads ... variable. This means that it is accessible in any function of the program. Unlike local variables, a global variable does not disappear at the end of a function....
🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c programming scope rules
C Programming Scope Rules
June 10, 2012 - There are three places where variables can be declared in C programming language − · Inside a function or a block which is called local variables. Outside of all functions which is called global variables.
🌐
DataFlair
data-flair.training › blogs › variable-scope-in-c-local-vs-global
Variable Scope in C – Local vs Global - DataFlair
February 7, 2024 - Variables have their scope determined at the time of declaration and can be categorized into two primary types: Global Variables: These variables are declared outside of any specific functions, making them accessible throughout the entire program.
🌐
Northern Michigan University
nmu.edu › Webb › ArchivedHTML › MathComputerScience › c_programming › c_046.htm
Local and Global variables
Local These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed.
🌐
WsCube Tech
wscubetech.com › resources › c-programming › global-variables
Global Variables in C Programming (With Examples)
1 week ago - Learn in this tutorial about C global variables with examples. Understand their scope, usage, benefits, and limitations. Read now!
🌐
Reddit
reddit.com › r/learnprogramming › [c] global variables vs local variables. best practice?
r/learnprogramming on Reddit: [C] Global variables Vs local variables. Best practice?
May 17, 2016 -

As the title states, really. Is there a "preferred" practice of how you declare your variables, or is it all just personal preference?

For instance I currently have a lot of global variables in my code, but they're all used all over the place in many different functions so to me it makes sense for the majority of them to be global. Also, my code is for embedded systems.

From what I can see though, people seem to think it's better to use local variables over global variables, even though this would surely take tons of re declaring and complex function arguments to keep variables across multiple functions.

Am I just not thinking about this the right way? Are there pros and cons of both global and local variables?

Edit: Forgot to mention that the C code is used in Atmel uC's, and is Embedded.

Top answer
1 of 5
11
Ideally, you should have no global variables in your code. This is not a matter of preference - using globals makes anything but toy programs almost impossible to either understand, test, or actually get working.
2 of 5
4
Global variables, or external variables in C parlance, might seem great. Shorter argument lists, the variable is always there when needed! But the variable is also there when you don't want it, they connect the code in ways that aren't obvious and create dependencies. Look at a function that changes a global. Is your change to the variable bad for some other function? Was some other code expecting it not to change? Sure, maybe your text editor can find all the references, but no one would remember to look every time. Those functions that use globals are harder to modify and reuse also. You wrote a great function that uses a global and want to reuse it, but oops it uses globals so can only be used for changing those globals and doesn't have as much generality. Your code with variables used all over the place would be a smell to me. I'd slowly try to rewrite the globals out if I could. Do you use a global in only one function just because you want the value to persist between calls of that function? Then use a static local variable. There's also the problem of interpositioning, your global being used instead of one in a library. And this happens with functions. "But aren't functions always global in C?" Yes, but you can make your functions static and they won't be visible outside of its translation unit. However, I wouldn't worry too much about that.
Find elsewhere
🌐
Medium
medium.com › @firatatalay › essential-guide-to-variables-in-c-programming-understanding-local-global-and-static-33aede116f32
Essential Guide to Variables in C Programming: Understanding Local, Global, and Static | by Firat Atalay | Medium
July 30, 2024 - When you declare a variable like int A, it reserves a chunk of addresses in this memory. int A; // Declaration of a variable A = 5; // Definition of a variable · In this example, int is the data type, and A is the variable name that can hold an integer value, such as 5.
🌐
Dot Net Tutorials
dotnettutorials.net › home › local vs global variables in c
Local Vs Global Variables in C Language - Dot Net Tutorials
December 2, 2023 - Lifetime: Local variables exist ... Scope and Accessibility: Local variables have a limited scope confined to their function or block, while global variables are accessible throughout the program....
🌐
SlideShare
slideshare.net › home › software › scope rules : local and global variables
Scope rules : local and global variables | PPTX
[1] Local variables are declared within a function and can only be accessed within that function. [2] Global variables are declared outside of functions and can be accessed anywhere.
🌐
Medium
medium.com › @aserdargun › introduction-to-c-global-local-and-static-global-variables-d3bb76ca012c
Introduction to C — Global, local and static global variables
October 30, 2023 - In contrast with global variables, temporary variables that are used to solve a problem should be declared as local: the memory is allocated for these, and when they go out of scope, the memory is no longer allocated.
🌐
Quora
quora.com › What-is-the-difference-between-local-variable-and-global-variable-in-C
What is the difference between local variable and global variable in C? - Quora
Answer (1 of 22): A global variable is defined outside the scope of any functions or structs; that is outside of any braces. A local variable is defined within a function’s definition. Like so: [code]int global; int *global_pointer; void foo(int arg) { int local; if (arg == 0) { local = 3; ...
🌐
Scaler
scaler.com › home › topics › what is global variable in c?
What is Global Variable in C? - Scaler Topics
May 21, 2024 - We can not redefine the value of ... variable in the global scope. If a local variable and a global variable have the same name, we can access the global variable by using the extern keyword....
🌐
Codingunit
codingunit.com › c-tutorial-functions-and-global-local-variables
C Tutorial – Functions and Global/Local variables » CodingUnit Programming Tutorials
Note: As you can see there is not an int before our_site() and there is not a return 0; in the function. The function can be called by the following statement: our_site(); A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside ...
🌐
University of Texas
farside.ph.utexas.edu › teaching › 329 › lectures › node19.html
Global variables
The C compiler recognizes a variable as global, as opposed to local, because its declaration is located outside the scope of any of the functions making up the program. Of course, a global variable can only be used in an executable statement after it has been declared.
🌐
Guru99
guru99.com › home › software engineering › difference between local and global variable
Difference between Local and Global Variable
July 28, 2025 - The local variable is declared inside a function, whereas the Global variable is declared outside the function. Local variables are created when the function has started execution and is lost when the function terminates, on the other hand, ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › global-variables-in-c
Global Variables in C - GeeksforGeeks
The Declaration of a global variable is very similar to that of a local variable. The only difference is that the global variable is declared outside any function. We can take an example by assuming that we have a chair at our house and one ...
Published   July 23, 2025