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[C] Global variables Vs local variables. Best practice?
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.comIs there a difference between a global variable and a static variable in a function in terms of what the compiler produces?
Passing a Global Variable as Function Parameter Across Multiple Files in C
Videos
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.
This is not an answer. This is a third option, that is sort of intermediate:
{
int x;
for (x = 0; x < 100; x++) {
// do something
}
// use x
}
This is equivalent to the 2nd option, if there's no code between the two closing brackets.
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.