c - 在 C99 模式之外使用的“for”循环初始声明

标签 c dev-c++

知道我为什么会收到该错误吗?我以为我做的一切都很好,在我编码过去之前它没有显示任何错误?

for (int i=0;i<height;i++)
{
    if (i == 0 || i == height-1 )
    {
        for (int j=0;j<width;j++)
        {
            printf("%i_",paddings);
            if (j == width-1)
            {
                printf("%i\n",paddings);
            }
        }
    }

最佳答案

这是 ANSI C 时代的阻碍。基本上,这意味着您必须像这样声明要在 for 循环中使用的变量:

// Looping variables declared outside the loops
int i, j;

for (i = 0; i < n; i++)
{
    if (i == 0 || i == height - 1)
    {
        for (j = 0; j < width; j++)
        {
            printf("%i_", paddings);
            if (j == width - 1)
            {
                printf("%i\n", paddings);
            }
        }
    }
}

或者,您可以更改编译器标志,使其使用 C99 或更高版本。对于 gcc,这只需要添加编译标志 -std=c99,或者对于 C11,-std=c11

关于c - 在 C99 模式之外使用的“for”循环初始声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30159997/

相关文章:

c++ - 如何恢复 C++ 内置头文件?

c - 在键盘输入时无限循环停止时执行 - C

无法打开输出文件 helloWorld.exe : Permission denied

c++ - 如何从外部函数 C++ 访问动态结构?

c - 分配给具有数组类型的表达式并请求非结构或 union 中的成员

c++ - 为什么我在 dev c++ 中使用 "gets function"后不能使用 "cin function"?

c - undefined reference #include <iconv.h> ,`libiconv_open' ,`libiconv' ,`libiconv_close'

c - 在 C 中调用函数时的序列点和未定义/未指定的行为

c - 如何使用 qemu 为 CPU 建模?

c - 对链接列表使用按引用传递