On Fri, 28 Aug 2015, Petr Cech wrote:
Hi everyone,
I would like to ask you what you think about the initialization of
iterative variables in forloops. I know that present code style does
not allow it. But how I recognized, we use C99, and this feature is
here now.
(example)
Instead of:|
|||# inti;
# for(i =0;...)|||
we could write:
||# for(inti =0;...)|
I see an advantage in limiting the validity of such variables. That
means higher code readability. Disadvantages I searched but did not
find.
What this misses is a use case of indexed searches where resulting index
value is used beyond the loop itself. By changing context of variable
declaration, you make variable inaccessible outside of the loop.
According to C standard, declaration expression in for() should only be
used to declare identifiers for objects having storage class auto or
register and "the scope of any identifiers it declares is the remainder
of the declaration and the entire loop, including the other two
expressions" (6.8.5.3 of C11 standard, C99 had similar sentence).
--
/ Alexander Bokovoy