在纯C(前C99)中声明循环变量的好方法?

Ale*_*xRu 0 c loops for-loop c89

正如您在C中所知,您不能在for循环的初始化中声明循环变量而不是C++.在纯C中声明循环变量的熟练/好的方式是什么?全球化是否更好?

  int i = 0;
  for (i = 0; fmla[i] != '\0'; i ++) {
    //do something
  }
Run Code Online (Sandbox Code Playgroud)

Jea*_*nès 5

在C99之前,您可以将函数头部中的控制变量声明为其他变量或使用内部块:

//some code
{
  int i;
  for (i=0; ...; ...) {
  }
}
// some code
Run Code Online (Sandbox Code Playgroud)

这避免与其他控制变量冲突,并为您提供类似于C99 for循环的语义.