gpt4 book ai didi

c - 如何修复 “for loop initial declaration used outside C99 mode” GCC 错误?

转载 作者:太空宇宙 更新时间:2023-11-04 06:18:33 24 4
gpt4 key购买 nike

我正在尝试解决,但我不知道哪里出错了。

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

最佳答案

您在 for 循环中声明了您的 i 变量。这在 C++ 中很常见,但在 C99 规范中添加了(令人惊讶的是最近)。

i 变量的声明移到 for 循环之外:

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

或者,您可以告诉 GCC 在 C99 模式下编译您的代码:

gcc -std=c99

或者,如果您想保留 GCC 特定的功能,请使用:

gcc -std=gnu99

关于c - 如何修复 “for loop initial declaration used outside C99 mode” GCC 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40303668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com