search

Home  >  Q&A  >  body text

c++ - 关于C语言块作用域于for循环,本人小白

请问如下代码:
for(int i = 1;i <= 5;i++)
{

 i = i;

}

其中定义的i属于块作用域吗?i的作用域是?为啥我在某些编译器能过,有些就不能过呢?求教大神

迷茫迷茫2807 days ago567

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-04-17 15:28:03

    This syntax is not standard syntax, and there is no guarantee that all compilers can compile this sentence, but if it can be compiled, i belongs to the block scope.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:28:03

    It should be a compiler problem. VS2010 may use the c90 standard. C90 does not allow direct declaration of int i=0 in for. Int i must be declared at the beginning of the program. C99 supports writing for like this. If it fails to compile, this may be the problem.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:28:03

    In the gcc compiler, the c language must first define a statement before it can be called. The mingw compiler can declare variables anywhere in the middle of the code

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:28:03

    Non-standard syntax, compilers are different, and the results are not uniform. However, this question does not make sense. Modifying loop variables in the loop body is against programming standards

    reply
    0
  • Cancelreply