The main benefit of option 1 is that you can use x outside the body of the loop; this matters if your loop exits early because of an error condition or something and you want to find which iteration it happened on. It's also valid in all versions of C from K&R onward.

The main benefit of option 2 is that it limits the scope of x to the loop body - this allows you to reuse x in different loops for different purposes (with potentially different types) - whether this counts as good style or not I will leave for others to argue:

for ( int x = 0; x < 100; x++ )
  // do something

for ( size_t x = 0; x < sizeof blah; x++ )
  // do something;

for ( double x = 0.0; x < 1.0; x += 0.0625 )
  // do something

However, this feature was introduced with C99, so it won't work with C89 or K&R implementations.

Answer from John Bode on Stack Overflow
๐ŸŒ
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):
๐ŸŒ
Google
google.github.io โ€บ styleguide โ€บ cppguide.html
Google C++ Style Guide
More formally it means that the type has no user-defined or virtual destructor and that all bases and non-static members are trivially destructible. Static function-local variables may use dynamic initialization. Use of dynamic initialization for static class member variables or variables at namespace scope is discouraged, but allowed in limited circumstances; see below for details. As a rule of thumb: a global variable satisfies these requirements if its declaration, considered in isolation, could be constexpr.
Discussions

[C] Global variables Vs local variables. Best practice?
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. More on reddit.com
๐ŸŒ r/learnprogramming
28
6
May 17, 2016
Is it bad to use global variables?

This question has so much to go on it hurts even before starting.

The general notion is that you shouldn't get into the habit of doing it because it's very often misused. You can employ the feature correctly, but "time has shown" that even when people think they're using it correctly, they're not.

It's like goto, but less extreme. Should you use goto? Well, there are cases in which it's a good tool for the job. But the general advice is to avoid it because it's often not what you want (for reasons I won't go into in here -- not because there aren't any, but because it'd make the text way longer).

Short list of problems with globals:

  • Fixed dependencies. Your program depends on one stuff. If something changes and now you need many __stuff__s, you're screwed. All your code has an implicit dependency on one stuff.

  • Global mutable state. If you have several functions mutating this variable, is it all done in the right way? If one function changes, do many of the other ones also have to be adjusted to fit the new way in which your globals are used?

  • Are your functions still re-entrant? That is, say F uses stuff and calls G, which in turn calls F. This second call to F is happening before the first one returns. Is your use of stuff compatible with this behavior? There are cases in which the use of globals helps with having this problem (but not just globals; consider static locals in C).

  • (This one is kind of a cheat, but bear with me.) Why not make stuff local to main? =D You'd be surprise how many times globals are simply not needed by making it local to some function.

Some features have their use case. The point is that there is a group of features which are generally harmful when developing large/complex systems. So even though you can make them work, just don't rely on them because they're too much error prone. Douglas Crockford is famous for pointing out these kinds of features. I think it could be helpful if you were to watch some of his talks (many are on youtube).

((EDIT)) Notice how many of these problems simply barely can be called problems in small programs. ((/EDIT))

((EDIT2)) By the way, there are more reasons. Just to be clear, that was a SHORT list. ((/EDIT2))

((EDIT3)) u/tourn mentioned a link I think is important, which is this => http://c2.com/cgi/wiki?GlobalVariablesAreBad ((/EDIT3))

More on reddit.com
๐ŸŒ r/learnprogramming
13
17
February 16, 2013
Is there a difference between a global variable and a static variable in a function in terms of what the compiler produces?
One big difference is that the "static int test = 1 " variable in void t1() is just in the scope of the function, no other function can see this value but the global "int test = 1" is visible for all other functions, below the definition and declaration and just because this behaviour it makes sense that the compiler treats both variable differently More on reddit.com
๐ŸŒ r/C_Programming
4
4
January 28, 2018
Passing a Global Variable as Function Parameter Across Multiple Files in C
I don't think I understand your question, could you provide some example code? What kind of errors are you getting? At compilation or run-time? I assume what you mean is that you've implemented a linked list in a single source file where a global variable is being used by nearly every function. In trying to write more modular code you've elected to split the source into multiple files and headers, but those now external functions can't access the global variable you've declared in the file where you've defined main. If that's correct, my solution would be to wrap the global variable into the struct for the linked list since I assume you'd be passing that to all these functions anyway. If I'm incorrect and you're stuck with what you describe above I recommend double checking your: function declarations: have you added this new parameter to the list in your declarations? Same goes for definitions. variable definition: have you defined a variable in main to pass to your functions? compilation: have you updated your makefile or compilation command to reflect the changes in the structure of your code? Hope this helps. EDIT: formatting, clarification. More on reddit.com
๐ŸŒ r/C_Programming
19
12
December 8, 2016
๐ŸŒ
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....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ what-are-local-variables-and-global-variables-in-cplusplus
What are local variables and global variables in C++?
June 4, 2025 - #include <iostream> using namespace std; // Global variable declaration: int g = 20; int main () { // Local variable declaration: int g = 10; cout << g << endl; // it will call local variable cout << ::g << endl; // using the scope resolution operator will give you access to call the value ...
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ local-global-and-static-variables-in-c โ€บ index.html
Local, Global and Static variables in C - C Programming Tutorial - OverIQ.com
For example: In this case variables a and b inside function_1() are local to function_1(), while variables a and b inside function_2() are local to function_2(). They are entirely independent of each other. If you change the value of a inside the function_1() then it will not change the value ...
๐ŸŒ
Code Quoi
codequoi.com โ€บ en โ€บ local-global-static-variables-in-c
Local, Global and Static Variables in C - codequoi
This is why we need to be able to distinguish between local, global and static variables when we program in C. Local variables are very short-lived. Declared inside a function, they only exist in RAM as long as the function exists. The second their function ends, they disappear! Letโ€™s create a variable named a in a function as an example, and letโ€™s try to print it from a different function:
Find elsewhere
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 172694247 โ€บ c-Variables
C Variables: Local vs Global | PDF
Please check your connection, disable any ad blockers, or try using a different browser.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ cprogramming โ€บ c programming scope rules
C Programming Scope Rules
June 10, 2012 - A program can have same name for local and global variables but the value of local variable inside a function will take preference. Here is an example โˆ’
๐ŸŒ
NVIDIA
docs.nvidia.com โ€บ cuda โ€บ cuda-c-programming-guide
CUDA C++ Programming Guide (Legacy) โ€” CUDA C++ Programming Guide
A kernel is defined using the __global__ declaration specifier and the number of CUDA threads that execute that kernel for a given kernel call is specified using a new <<<...>>>execution configuration syntax (see Execution Configuration). Each thread that executes the kernel is given a unique thread ID that is accessible within the kernel through built-in variables.
๐ŸŒ
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.
๐ŸŒ
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 - This memory location has an address, and the variable name is a way to reference that address. For example, consider a main memory with addresses from 0, 1, 2, 3, and so on.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is global variable in c?
What is Global Variable in C? - Scaler Topics
May 21, 2024 - From the above example, one global variable could assign value to another global variable. This means we could not change or redefine the global variables in the global scope, but we could use global variables in the global scope. Note: We should always initialize global variables with constant values, or the compiler automatically assigns 0 to it. We know that local variables have higher precedence (priority) than global variables.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ c programming tutorial โ€บ local variable in c
Local Variable in C | How Local Variable Works in C with examples?
April 3, 2023 - In the above code using both global variable (value1,value2) and local variables (a,b) are used respectively. The global variables are accessed anywhere of the program, so they are using within function DisplayValues() and main().
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Scribd
scribd.com โ€บ document โ€บ 406687614 โ€บ unit-3
Local Variable Scope in C Functions | PDF
Please check your connection, disable any ad blockers, or try using a different browser.
๐ŸŒ
Processing
processing.org โ€บ examples โ€บ variablescope
Variable Scope / Examples / Processing.org
Variables are localized * within each block, the space between a { and }. */ int a = 80; // Create a global variable "a" void setup() { size(640, 360); background(0); stroke(255); noLoop(); } void draw() { // Draw a line using the global variable ...
๐ŸŒ
Khan Academy
khanacademy.org โ€บ computing โ€บ computer-programming โ€บ programming โ€บ functions โ€บ pt โ€บ local-and-global-variables
Local and Global Variables | Functions | Intro to JS
Learn for free about math, art, computer programming, economics, physics, chemistry, biology, medicine, finance, history, and more. Khan Academy is a nonprofit with the mission of providing a free, world-class education for anyone, anywhere.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ scope-of-variables-in-c-local-and-global-scope-explained
Variable Scope in C โ€“ Local and Global Scope Explained
September 8, 2021 - This program compiles without any ... from my_func() as well and its value is 7 ยท In this example, there are two functions โ€“ the main() and my_func(). However, the variable my_num is not local to any function in the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ variables-in-c
Variables in C - GeeksforGeeks
When a variable is declared, the compiler is told that the variable with the given name and type exists in the program. But no memory is allocated to it yet. Memory is allocated when the variable is defined. Most programming languages like C generally declare and define a variable in the single step. For example, in the above part where we create a variable, variable is declared and defined in a single statement.
Published ย  October 17, 2025
๐ŸŒ
Codecademy
codecademy.com โ€บ forum_questions โ€บ 55ac76c776b8fea47a000184
10/13 - Global vs Local variables. I understand how, but not WHY. | Codecademy
The main variables are global. We would run addToTotal() on all purchases, then run addShipping() before adding tax, then finally, addTax() to the calculated total for a grand total.