I have a problem in understanding the difference in scope of initial variable of for loop in those 4 languages C, C++, C#, Java
我在理解4种语言C,C ++,C#,Java中for循环的初始变量范围的差异时遇到了问题
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.
我所知道的是在C#:for(int i = 1; i <= 5; i ++)这里,我是一个局部变量剂量不会出现在括号的范围之外。所以我可以在多个块中多次使用它,Java也是这样,但是我知道c和c ++与我的in(int i = 1; i <= 5; i ++)不同不在里面的水平。
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++.
我认为C99,C ++ 11,C#,Java是相同的,而差异仅在旧的C和C ++中。
Is this right or not ??
这是对还是不对?
Thanks
2 个解决方案
#1
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 ++(在1998年成为ISO标准的ANSI标准之前)才符合您对“旧C ++”的描述。这在大约1995年的草案标准中从记忆中发生了变化,当时大多数编制者采用了这种变化。在此之前,一些C ++编译器将变量范围限制为循环,作为特定于供应商的扩展。
C from the 1999 standard, standard C++, Java, and C# all limit scope of variables declared within a for loop to only that loop.
来自1999标准的C,标准C ++,Java和C#都将for循环中声明的变量范围限制为仅该循环。
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.
1989年的标准C(ANSI)和1990(ISO),预标准C(包括K&R)和1995年以前的标准C ++都没有。正如juanchpanza在评论中指出的那样,C89 / C90根本不允许在循环初始化中进行变量声明。
#2
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
实际上,对于预先标准化的C ++来说,它并不完全正确。因为一些编译器有关于循环变量的问题,我可以在for循环块之外使用而不需要重新声明。我的意思是
// 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
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 ++(在1998年成为ISO标准的ANSI标准之前)才符合您对“旧C ++”的描述。这在大约1995年的草案标准中从记忆中发生了变化,当时大多数编制者采用了这种变化。在此之前,一些C ++编译器将变量范围限制为循环,作为特定于供应商的扩展。
C from the 1999 standard, standard C++, Java, and C# all limit scope of variables declared within a for loop to only that loop.
来自1999标准的C,标准C ++,Java和C#都将for循环中声明的变量范围限制为仅该循环。
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.
1989年的标准C(ANSI)和1990(ISO),预标准C(包括K&R)和1995年以前的标准C ++都没有。正如juanchpanza在评论中指出的那样,C89 / C90根本不允许在循环初始化中进行变量声明。
#2
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
实际上,对于预先标准化的C ++来说,它并不完全正确。因为一些编译器有关于循环变量的问题,我可以在for循环块之外使用而不需要重新声明。我的意思是
// 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.
在那之后我现在在其他地方。