0

let's assume that it is not only in visual studio but also in C99, C11 and etc.

there are two different ways of declaring variable "i" in for statement.

1) 
int i;
for(i = 0 ; i < index ; ++i) 

2)
for(int i = 0 ; i < index ; ++i)

Both work same. but I think there will be some difference between them. Do you have any idea about that? If yes, please let me know.

I just wondering about your opinion, and how it works differently.

Sorry. for answers, I know that the scope of "i" is different.

Is there any difference in view of system(I mean memory or etc.) or compiler work differently or assembled code is different or something like this.

Steve YonG
  • 80
  • 7
  • Oh I didn't find that Question. I'll check that one. thanks. – Steve YonG Feb 11 '16 at 07:29
  • 1
    A C++ post might not be the best duplicate, since C++ has always allowed declarations inside loops. In ancient versions of C, that was not allowed. – Lundin Feb 11 '16 at 07:36

4 Answers4

2

The only difference is that in the first case, the variable i is outside for scope so you could use it later on. There are no differences in term of efficiency.

If you use i only once, then definitely the 2nd case is better:

for(int i = 0 ; i < index ; ++i)

If you have loops that use index i, then it might make sense declaring it outside all loops.

But generally, the rule is to limit the scope of the variable - so the 2nd case is better. It's usually safer to limit the scope of the variable.

It'd worth noting that the 2nd case syntax only works with C99 or newer C11 (did not work with old C89). So some compilers would complain if you declare variable inside the loop. For example, gcc requires explicit flag -std=c99 to allow that syntax.

artm
  • 17,291
  • 6
  • 38
  • 54
  • It's not just the scope. It's also the lifetime which is different. Besides, the second syntax also works with C11. – undur_gongor Feb 11 '16 at 08:10
  • @undur_gongor - for automatic variables like `i`, lifetime is limited to its scope.Yes C11 works, my point is that it doesn't work with compiler. Will edit it now -thanks – artm Feb 11 '16 at 08:19
1

The scope and lifetime of i is different.

In the second example it is just inside the loop body. In the first, it extends beyond.

Apart from that, they are equal.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
1

Declaring a new variable in the initialization of the for loop is a C99 extension.

C89 requires that variables be declared at the beginning of a block.
Semantically, declaring variables in the initialization portion of the loop would limit the variables' scope to the body of the loop.

Limiting the scope is often desired to avoid misuse of variables after the body of the for loop has executed. For example, if you are doing a simple iteration, you may not want your index to exist after the for loop.

There is no right answer on which to use. The question becomes what you want your scope to be, and what compilers/language versions you intended to support.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
-2

In C99 the correct way is No 1). It requires the variables are declared before use. Looks like your compiler supports several standards, therefore it looks transparent to you what construction to use, and they all result in the same behavior. My personal preference in this case is 2) because it reduces scope of the variable i, and also prevents from unitialized value use (less risky).

dmi
  • 1,424
  • 1
  • 9
  • 9
  • 1
    1st case has more big scope. but how does it reduce scope of variable? I cannot understand. can you explain more? – Steve YonG Feb 11 '16 at 07:34
  • "My personal preference in this case is 1) because it reduces scope of the variable i, and also prevents from unitialized value use (less risky)" Eh? – Lundin Feb 11 '16 at 07:38
  • @SteveYonG In the second case *i* is undeclared after the loop. You cannot use it there. – August Karlstrom Feb 11 '16 at 07:48
  • Oh thanks for the comments, of course it was a typo - I fixed my answer. – dmi Feb 11 '16 at 08:34
  • 1
    In the case 1) the scope of the variable i is more than the for loop. Thus it may be reused after for if required. From the other hand, if the reuse of it is made with assumption it was initialized within the for loop, it may give an unexpected behavior if the assumption is wrong in some reason (i.e. if the use of `i` was removed from the for loop with some changes). That;s why it is more risky. – dmi Feb 11 '16 at 08:38