相关疑难解决方法(0)

C中的变量声明放置

我一直认为在C中,所有变量都必须在函数的开头声明.我知道在C99中,规则与C++中的规则相同,但C89/ANSI C的变量声明放置规则是什么?

以下代码使用gcc -std=c89和成功编译gcc -ansi:

#include <stdio.h>
int main() {
    int i;
    for (i = 0; i < 10; i++) {
        char c = (i % 95) + 32;
        printf("%i: %c\n", i, c);
        char *s;
        s = "some string";
        puts(s);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不应该在C89/ANSI模式下声明cs导致错误吗?

c declaration c89

121
推荐指数
5
解决办法
8万
查看次数

C:for循环初始化声明

有人可以详细说明以下gcc错误吗?

$ gcc -o Ctutorial/temptable.out temptable.c 
temptable.c: In function ‘main’:
temptable.c:5: error: ‘for’ loop initial declaration used outside C99 mode
Run Code Online (Sandbox Code Playgroud)

temptable.c:

...
/* print Fahrenheit-Celsius Table */
main()
{
    for(int i = 0; i <= 300; i += 20)
    {
        printf("F=%d C=%d\n",i, (i-32) / 9);        
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:我含糊地回忆起int i应该在for循环之前声明.我应该声明我正在寻找一个给出C标准历史背景的答案.

c gcc for-loop syntax-error

43
推荐指数
2
解决办法
6万
查看次数

标签 统计

c ×2

c89 ×1

declaration ×1

for-loop ×1

gcc ×1

syntax-error ×1