有人能详细解释一下以下的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
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);
}
}
P.S: 我模糊地记得在 for
循环之前应该声明 int i
。我应该说明我正在寻找一个能够提供 C 标准历史背景的答案。
(5 * (i - 32)) / 9
(少了5
)。通常,你需要浮点数运算-但是你还需要把你的printf格式从C =%d
改为C =%./ 2f
或类似的方式(并且你要用(5.0 * (i - 32)) / 9.0
进行转换)。 - Jonathan Leffler