简体   繁体   English

如何解决“在C99模式外部使用的for循环初始声明” GCC错误?

[英]How do I fix “for loop initial declaration used outside C99 mode” GCC error?

I'm trying to solve but I don't know where a mistake. 我正在尝试解决,但我不知道哪里有错误。

int main() {
    for (int i = 0; i < 3; i++) {
        pid_t pid = fork();
  }
    return 0;
}

You're declaring your i variable inside the for loop. 您在for循环中声明了i变量。 This is common in C++, but was added (surprisingly recently) in the C99 specification. 这在C ++中很常见,但是在C99规范中添加(令人惊讶地是最近)。

Move the declaration of your i variable outside of the for loop: i变量的声明移到for循环之外:

int main() {
    int i;
    for (i = 0; i < 3; i++) {
        pid_t pid = fork();
  }
    return 0;
}

Alternatively, you can tell GCC to compile your code in C99 mode: 或者,您可以告诉GCC在C99模式下编译代码:

gcc -std=c99

Or if you want to retain GCC-specific features, use: 或者,如果您想保留特定于GCC的功能,请使用:

gcc -std=gnu99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM