刚刚看C primer Plus 6 Edition 12章:Storage Classes 里的scope这样说
Traditionally, variables with block scope had to be declared at the beginning of a block. C99
relaxed that rule, allowing you to declare variables anywhere in a block. One new possibility is in the control section of a for loop. That is, you now can do this:
for (int i = 0; i < 10; i++)
printf("A C99 feature: i = %d", i);
As part of this new feature, C99 expanded the concept of a block to include the code controlled by a for loop, while loop, do while loop, or if statement, even if no brackets are used. So in the previous for loop, the variable i is considered to be part of the for loop block. Therefore, its scope is limited to the for loop. After execution leaves the for loop, the program will no longer see that i .
于是我试了下
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 10; i++)
printf("A C99 feature: i = %d", i);
return 0;
}
编译后提示
error: 'for' loop initial declaration used outside C99 mode
也就是说C99不允许这样做的,不明白到底是哪个错了
![](http://imgsrc.baidu.com/forum/w%3D580/sign=640a4ba51bd8bc3ec60806c2b28aa6c8/24a2e6cd7b899e510738c48b40a7d933c9950d56.jpg)
Traditionally, variables with block scope had to be declared at the beginning of a block. C99
relaxed that rule, allowing you to declare variables anywhere in a block. One new possibility is in the control section of a for loop. That is, you now can do this:
for (int i = 0; i < 10; i++)
printf("A C99 feature: i = %d", i);
As part of this new feature, C99 expanded the concept of a block to include the code controlled by a for loop, while loop, do while loop, or if statement, even if no brackets are used. So in the previous for loop, the variable i is considered to be part of the for loop block. Therefore, its scope is limited to the for loop. After execution leaves the for loop, the program will no longer see that i .
于是我试了下
#include <stdio.h>
int main(void)
{
for (int i = 0; i < 10; i++)
printf("A C99 feature: i = %d", i);
return 0;
}
编译后提示
error: 'for' loop initial declaration used outside C99 mode
也就是说C99不允许这样做的,不明白到底是哪个错了
![](http://imgsrc.baidu.com/forum/w%3D580/sign=640a4ba51bd8bc3ec60806c2b28aa6c8/24a2e6cd7b899e510738c48b40a7d933c9950d56.jpg)