The advantage of approach 1 is a slightly smaller file size due to less text characters in the source code:
int i, j; for (i = 0; i < numRows; i++) for (j = 0; j < numCols; j++) //<some code here>
The advantage of approach 2 is the smaller scope of local variables.
int i; for (i = 0; i < numRows; i++) { int j; for (j = 0; j < numCols; j++) //<some code here> }
Even if the differences in optimizations are negligible in today's modern computers, which approach is considered "better" code?
Edit to clarify that this question is not a duplicate:
This question is based on the current C11 standard, which does not allow for syntax like this:
for (int i = 0; i < numRows; i++)
In C++ and C99, this syntax is perfectly acceptable whereas C11 does not allow for variable declarations inside the for
statement.
Edit to correct misinformation:
I thought I was using C11 because I had recently downloaded the compiler from CodeBlocks, so that's why I said C11 didn't allow for variable declarations inside the for
statement. But it turns out I was actually using C90, which was the root of my problems.