2

I am quite new to C but have a lot of experience in C#. My college instructor told me that in "pure" C, it is wrong to initialize the loop variable inside the loops parantheses. He said that it runs because of the VS compiler. For some reasons, all the material in the presentation also shows loops with their loop variable declared outside the parantheses.

for (int i=0; i < 5; i++)
{
   //He says that this is wrong, and you will lose points in tests for that
}

int i;
for (i=0; i < 5; i++)
{
   //Says it should be done like that (i declared outside loop)
}

Does it really matter? Do some compilers fail to recognize it? Will I lose points on a test?

Algo
  • 178
  • 7
  • 2
    This question will explain you everything.[C: for loop int initial declaration](https://stackoverflow.com/questions/1287863/c-for-loop-int-initial-declaration) **TLDR**: Loop variable initialization inside the loop is allowed since C99. – Roberto Caboni Apr 06 '20 at 12:14
  • 1
    C99 draft standard n1256: *6.8.5.3 The for statement [...] If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions[...]*. Doing this has been perfectly OK in standards-compliant compilers for a *long time*. – EOF Apr 06 '20 at 12:14
  • 1
    Your college seams uninformed, the first loop perfectly compiles and runs with other compilers too. _"Will I lose points on a test?"_ that depends on how strict your lecturer enforces his style of coding. That, you should ask your lecturer not SO. – Ackdari Apr 06 '20 at 12:21
  • 1
    What does the college mean by "pure" C? Historic C, rather than to an evolving standard? Their course material should evolve too, but sadly at my school, some of the older teachers were giving exactly the same lessons as they did 20 years previously. Even with "history", its known facts and their interpretation changes. – Weather Vane Apr 06 '20 at 12:25
  • @WeatherVane "*Their course material should evolve too, but sadly at my school, some of the older teachers were giving exactly the same lessons as they did 20 years previously.*" - Yes, I know this, too. It´s kind of the "*never touch a running system*" concept which is kind of controversy when working in the IT area. But it is also a matter of costs - f.e. study books have to be rewritten when the parts contradict to a newer standard. Nonetheless, an apprenticeship in programming in any language shall be "*up-to-date*". – RobertS supports Monica Cellio Apr 06 '20 at 12:48
  • @RobertS in those days there was no concept of [Continuing Professional Development](https://career-advice.jobs.ac.uk/career-development/what-is-continuing-professional-development-cpd/) but it was also the fault of those who set the syllabus. – Weather Vane Apr 06 '20 at 12:53
  • Are you declaring function parameters inside or after the parens? – stark Apr 06 '20 at 13:53

3 Answers3

4

It's definitely not wrong, but a matter of which C standard your compiler uses.

If your compiler uses a standard earlier then C99, then initialising a variable in the loop header will through an error like so.

$ gcc loop.c 
loop.c: In function ‘main’:
loop.c:5: error: ‘for’ loop initial declaration used outside C99 mode

In later standards its supported then. The only difference of both styles for your code then is the scope of the variable is limited to the loop if the declared in the loop header.

So, by not initializing in the loop header, you make your code a little more portable or standard/compiler independent. But most people I know certainly use both styles.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Paul Würtz
  • 1,641
  • 3
  • 22
  • 35
2

There is no difference between the two versions when compiling with a modern compiler which fits to the C99 standard or above instead of when defining i inside of the parentheses, i has its scope and lifetime only inside of the according loop.

Outside of the according loop, the i you use inside the loop does not exist. You can´t use it f.e. twice for another loop.

When you try to compile code on a compiler/ compiler version, that is build up to a C standard before C99 with the definition of i inside of the parentheses, you will get an compilation error, just because it was not introduced to the language yet.

Both techniques have their advantages and downsides.

It is a reason of what compiler or compiler version you use, if you want to use i after the relative loop and nonetheless a matter of style and personal taste.

Will I lose points on a test?

Well, if he/she explicitly insists on this one (even if you use a modern compiler), I would not try to bother him/her and just declare i before the relative loop and everything is fine.

It is also sometimes (if not common) prohibited to use techniques not learned in the class. It might be that this is a nit-picking but matching example for this.

2

It is OK in the newer C versions.

But there is a distinct difference. i has different scope.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • This so-called newer versions are over 20 years old. So newer than 1972 when the language came out, but definitely not new. – ikegami Apr 06 '20 at 13:29
  • some people still complain if it is not ANSI c. For them it is even to new and not mature enough :) – 0___________ Apr 06 '20 at 13:49