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.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › local-and-global-variables
Local and Global Variables - GeeksforGeeks
March 21, 2024 - In many programming languages, local variables have a limited visibility and lifespan compared to global variables, which are accessible from any part of the program. This encapsulation of variables within specific scopes helps to organize code, prevent unintended modifications, and manage memory efficiently. Here are the example ...
🌐
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
However, we can send its pointer to another function if need be, as we’ve seen with the local variables. For static variables declared outside any function, the static keyword restrains their scope to the file in which we declare them. The functions in the same file following the global static variable’s declaration will be able to use it, but we won’t be able to access it from another of the program’s files. If we return to our previous example of the global variable across two files, and if we transform the global in foo.c into a static, we will get compilation errors: “undefined reference to ‘a’”. This comes from the fact that we declare in main.c that there is an extern definition of a elsewhere in the program.
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().
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 ... example, there are two functions – the main() and my_func(). However, the variable my_num is not local to any function in the program. Such a variable that is not local to any function is said to have global scope and is called a global variable...
🌐
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.