I'm familiar with C++ and I've recently decided to learn C. The tutorial that I'm following often writes for loops like this:
int i;
for (i = 0; i < 5; i++)
{
printf("%d", i);
}
You can see that the counter (i) is declared outside of the for loop body. When writing it in C (and C++) I write:
for (int i = 0; i < 5; i++)
{
printf("%d", i);
}
I've researched this a bit and it seems like the latter was illegal in C89 and was only introduced in C99. However, the tutorial I'm using is based on C99 and I've also seen a lot of modern C code where the counter is still declared outside of the for loop body.
Therefore, the question that I'm asking is: is there any practical benefit to declaring the counter outside of the for loop body in C99? In other words, which way should I write it?
Note: I've seen that there's "similar questions" but most of them are asking why the counter is declared outside of the for loop body in some code rather than whether there's any benefit. With that being said, there was one similar question that was asking about the benefit but it was in C++ and I'm not sure if there's a difference between the two languages in this regard.