Search code examples
cloopsscopeundefined-behavior

Is the scope of a variable initialized in the arguments of a loop undefined behaviour or implementation defined behaviour in C?


for what i know until today, in C, if a loop, for/while has brackets, it has a scope right? i have trying some code in GCC and this appears to be the case, so for example:

for(unsigned char i=0;...;...){
   unsigned char j=0;
}
//Outside of the for scope:
unsigned char j=0;

Here we have no problem at all, but, what about the variables inside the arguments of a loop?

So for example, this code in GBD C COMPILER (Online):

switch(Maneral){
      case 1:
         for(unsigned char i=0x0A;i<=0x64;i=i+0x0A){
            indice++;
         }
      break;
      
      case 2:
         for(unsigned char i=0x0A;i<=0x64;i=i+0x0A){
            indice++;
         }
      break;
   }

Gives no error, and i thought that it gives no error because the variables initialized inside the arguments of the for 'share' the scope of the brackets of the respective for

At least that was my headcannon until today because i was programming some embedded code por a MCU using CCS C COMPILER; this compiler says to have '97% ANSI C compatibility', and in this compiler, the next code:

switch(Maneral){
      case M_CONICO:
         for(unsigned char i=0x0A;i<=0x64;i=i+0x0A){ unsigned char too=0;
            ActualizaValor(i,DataConico[index]);
            index++;
         }
      break;
      
      case M_CHICO:
         for(unsigned char i=0x0A;i<=0x64;i=i+0x0A){ //LINE 'X'
            ActualizaValor(i,DataChico[index]);
            index++;
         }
      break;
   }

Gives an error ont the line 'X' and it says Identifier is already used in this scope and for example if i change the "i" in the second for loop for 'j' it gives no error, and for variables declared/defined inside the brackets (and not in the argument of the loop) it has no problem (CCS C COMPILER)

So, the scope of a variable initialized inside the arguments of a loop like 'for()' is something like implementationdefined behaviour / undefined behaviour or it is thing of the 97% ANSI C Compatibility?

Thanks in advance!

EDIT: I know i can just declare/define 'i' outside the switch but i want to understand this beacuse i thought i was sure about what i knew


Solution

  • As of C99, variables declared in the init-clause of a for loop are scoped to the entire loop, with its lifetime ending when the loop completes, so the for loop roughly behaves as if there was an extra set of implied braces around it, e.g.:

    for(unsigned char i=0;...;...){
       unsigned char j=0;
    }
    

    behaves as if you typed:

    {
       unsigned char i=0;
       for(;...;...){
           unsigned char j=0;
       }
    }
    

    But this is a C99 (and later) feature, and something claiming "ANSI C compatibility" may be referring to the C89/C90 standard, when this feature of for loops was not part of the standard. And in fact, the CCS compiler is using that definition, as its compliance matrix is defined in terms of ANS/ISO 9899-1990, where the ANSI ISO/IEC 9899-1990 standard is the long official name for the C90 standard.