1

In C is there any difference between declaring all variables outside a loop versus some inside aside from the scope? Is one way faster than the other? For example:

int i;
for(i = 0; i < len; i++)
{
  int j;
  for(j = i; j < len; j++)
  {
    …

vs

int i, j;
for(i = 0; i < len; i++)
{
  for(j = i; j < len; j++)
  {
    …

On a conceptual level I have a problem because in the first example the same variable is being re-declared each iteration, isn't that inefficient?

Celeritas
  • 14,489
  • 36
  • 113
  • 194
  • 3
    Find the compiler option that makes your C compiler emit assembly (it is `-S` for GCC) and read the output. If the output is the same in both cases, it means that the performance is the same. – Pascal Cuoq Oct 05 '13 at 08:38
  • 1
    What kind of difference are you interested in? Aside from the [tag:performance] tag, you don't mention that. Because obviously there *is* at least one obvious difference: the source code is not the same. – stakx - no longer contributing Oct 05 '13 at 08:43
  • @stakx is the bottom sentence unclear? Doesn't the first way redeclare the variable each time? – Celeritas Oct 05 '13 at 08:48
  • Obviously, if you use an initializer, there's a difference. And, even though this question is about C, with C++ there may be a difference due to calling of the constructor/destructor. – Ambroz Bizjak Oct 05 '13 at 09:07
  • You basically say 1)if the code is different there's a difference 2)different programing languages do this differently – Celeritas Oct 05 '13 at 09:11

4 Answers4

2

It is good practice for restricting scope of variable inside that loop only. There is no difference declaring variable outside loop and inside loop from allocation point of view.

When you see disassembly your code,you should get same code for both cases.And you will exactly find out where the storage for variable is allocated.It is allocated outside the loop.

Arya
  • 371
  • 2
  • 17
1

In modern C (AKA C99 or C11) there is a better option

for(int i = 0; i < len; i++)
{
  for(int j = i; j < len; j++)
  {

that is to directly declare the loop variables inside the for-statement.

In simple cases as yours, there is no difference on any level, this will all compile to the same binary.

In more complicated cases, there might be one, since you would be "re-using" the same variable for different purposes. You can easily mix up things and drain an old value from a previous use into later, where you aren't expecting it.

Minor disgression: int is in most cases not the right type for loop indices that speak of "length" or similar. Indices shouldn't be negative and the width of the type should be such you can capture the size of any object. Modern C has size_t for that purpose.

To have that feature with gcc, you'd have to add the switch -std=c99 or use the executable name c99. clang and many other compilers on POSIX machines comply to C99 per default.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 2
    Be warned that there are still several compilers that do not support this syntax by default (or at all). With gcc, you must specify -std=c99. – willus Oct 05 '13 at 12:44
  • I don't know why you find it necessary to "warn" about that. gcc tells you that all by himself if you use that construct. – Jens Gustedt Oct 05 '13 at 20:25
0

Variable declared inside the loop (block) is accessible only to that block, i.e, it is local to that particular block (it is visible from its point of declaration to the end of the block which you can't access beyond this scope).
In the code

int i;
for(i = 0; i < len; i++)
{
   int j;
   for(j = i; j < len; j++)
   {
       …

j is a local variable and has block scope. After the end of for(i = 0; i < len; i++) you can't access j further in the program. All modification to j will remain within this scope.
If you are not taking scope of the variable into the account then declaring variables at any level makes no difference (but both codes are different here) in case of loop.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • Not for the outer `for` loop – P0W Oct 05 '13 at 08:36
  • @P0W; What? What is wrong with this? – haccks Oct 05 '13 at 08:38
  • may be I misread something but looks like you're saying in `for(int i = 0; i < len; i++) {}`, `i` can't be used outside `for` block ? – P0W Oct 05 '13 at 08:53
  • @P0W; Run this code`#include `int main () { for(int i = 0; i<5; i++) { int j = i; } for(int i = 0; i<5; i++) printf("%d",j); return 0; }` – haccks Oct 05 '13 at 09:04
  • I've no idea what are you trying to convey. And before you ask I didn't downvote any of the time – P0W Oct 05 '13 at 09:11
0

When you restrict scope, you make it easier for the compiler to optimize your code (e.g. decide what variables should be used in registers). A good compiler should not waste time "re-declaring" a variable at the beginning of the loop under optimized conditions.

willus
  • 501
  • 3
  • 7