1

I have a problem in understanding the difference in scope of initial variable of for loop in those 4 languages C, C++, C#, Java

What I know is that in C#: for ( int i = 1; i<=5; i++) here, i is a local variable dose not appear out the scope of the for brackets. So I can use it multiple times in multiple blocks, also Java is like that, but what I was know that c and c++ not the same as i in for ( int i = 1; i<=5; i++) is declared on the level of for not in inside it.

I tried to run the below code to check if my thought correct or not:

old C:

   // old C:  error: 'for' loop initial declarations are only allowed in C99 or C11 mode                                                                 
  // note: previous definition of 'i' was here 
    for ( int i = 0; i<=5; i++)  printf ("%d",i);
    for  ( int i = 5; i>=0; i--)  printf ("%d",i);
    printf ("%d",i);

C99:

    // error: 'i' undeclared (first use in this function) 
    for ( int i = 0; i<=5; i++)  printf ("%d",i);
    for  ( int i = 5; i>=0; i--)  printf ("%d",i);
    printf ("%d",i);

old C++:

    // error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]                                                                  
    for ( int i = 0; i<=5; i++)  cout << i << endl;
    for( int i = 5; i>=0; i--)  cout << i << endl;
    cout << i;

C++11:

      for ( int i = 0; i<=5; i++)  cout << i << endl;
      for( int i = 5; i>=0; i--)  cout << i << endl;
      cout << i; error: 'i' was not declared in this scope   

Java:

       for ( int i = 0; i<=5; i++)  System.out.println(i);
       for(  int i = 5; i>=0; i--)  System.out.println(i);
       System.out.println(i); // error: 'i' was not declared in this scope

C#:

    for ( int i = 0; i<=5; i++)  Console.Write(i); // 0 1 2 3 4 5 
    Console.WriteLine("");
    for ( int i = 5; i>=0; i--)  Console.Write(i); // 5 4 3 2 1 0
    Console.WriteLine(i); // error: 'i' was not declared in this scope

I think C99, C++11, C#, Java are same while the diffrence only in old C and C++.

Is this right or not ??

Thanks

8
  • 1
    Note your "old C++" is pre-standard "C++". This hasn't changed since the first standard. Basically, all behave the same, except that in C89 you can't declare i in the scope of the loop. Commented Jun 12, 2015 at 21:16
  • @juanchopanza: Do you mean you cannot declare inside the parenthesis or that it's scope will be that of the block the loop itself belongs to? Commented Jun 12, 2015 at 21:40
  • 1
    @Olaf i know that in C89 i can't initialize i inside the parenthesis, but my question is about the scope Commented Jun 12, 2015 at 21:43
  • 2
    @Marzouk: If you can't declare it inside the parentheses, then no scope is possible. In all the other languages, the scope is the complete for statement (that is, the test and advance expressions, and the body of the loop.) Commented Jun 12, 2015 at 22:35
  • 1
    @Olaf I mean in C89 you cannot declare inside the loop parenthesis. That makes the question of scope moot in this particular case. Commented Jun 13, 2015 at 5:51

2 Answers 2

4

Not quite.

Only prestandard C++ (before the 1998 ANSI standard which became the ISO standard) behaves as you describe for "old C++". That changed in draft standards in about 1995, from memory, and most compilers at that time adopted the change. Before that, some C++ compilers limited scope of variables to loops as a vendor-specific extension.

C from the 1999 standard, standard C++, Java, and C# all limit scope of variables declared within a for loop to only that loop.

Standard C in 1989 (ANSI) and 1990 (ISO), prestandard C (including K&R), and prestandard C++ before 1995 do not. As noted by juanchpanza in comments, C89/C90 does not permit variable declarations within loop initialisation at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it's exactly what i need to know
Note that C89 does not allow variables declared the loop initialization as in OP's code.
True, juan. I'll modify my post shortly to reflect that.
1

Actually, it is not right exactly for the pre-standart C++. Because some compilers have issue about loop variables which i can be used outside of the for loop block without redeclaration. I mean

// error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]                                                                  
for ( int i = 0; i<=5; i++)  cout << i << endl;
for( int i = 5; i>=0; i--)  cout << i << endl;
cout << i;

This may not be error in that compiler. And you may see this macro in files to protection.

#define for if(0) {} else for

After that now i is in else block.

1 Comment

Redefining language keywords, as in that macro, gives undefined behaviour in both C and C++. Although it often gives the "protection" you describe with real-world compilers/preprocessors, that is not actually guaranteed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.