简体   繁体   English

在C99模式之外使用'for'循环初始声明

[英]'for' loop initial declaration used outside C99 mode

Any ideas why I am getting that error? 有任何想法为什么我会收到该错误? I thought I'd done everything fine, it wasn't showing any errors before I coded past it? 我以为我做得很好,在我编码过去之前没有显示任何错误吗?

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);
            }
        }
    }

This is a hold-back from the days of ANSI C. Basically, it means that you must declare variables for use in for loops like so: 这是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);
            }
        }
    }
}

Alternatively, you could change your compiler flags so that it uses C99 or above. 另外,您可以更改编译器标志,使其使用C99或更高版本。 For gcc this would simply entail adding the compile flag -std=c99 , or for C11, -std=c11 对于gcc这仅需要添加编译标志-std=c99 ,对于C11,则需要-std=c11

for (int i = 0; ...)

is a C99 extension; 是C99扩展名; in order to use it you must enable it via specific compiler flags in gcc 为了使用它,您必须通过gcc中的特定编译器标志启用它

For old standards like C89 is: 对于像C89这样的旧标准是:

int i;
for (i = 0; ...)

You must declare your variables outside the loop. 您必须在循环外声明变量。

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

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