我在编译以下代码时遇到了问题:
#include <stdio.h>
#include <limits.h>
int main () {
printf("short: [%d,%d]\n",SHRT_MIN,SHRT_MAX);
printf("int: [%d, %d]\n",INT_MIN, INT_MAX);
printf("long: [%d, %d]\n",LONG_MIN,LONG_MAX);
int aa=017;
printf("%d\n",aa);
return 0;
}错误消息为:
1>c:\tic\ex1\ex2\ex2.c(12) : error C2143: syntax error : missing ';' before 'type'
1>c:\tic\ex1\ex2\ex2.c(13) : error C2065: 'aa' : undeclared identifier但是,对此进行编译就可以了:
#include <stdio.h>
#include <limits.h>
int main () {
int aa=017;
printf("short: [%d,%d]\n",SHRT_MIN,SHRT_MAX);
printf("int: [%d, %d]\n",INT_MIN, INT_MAX);
printf("long: [%d, %d]\n",LONG_MIN,LONG_MAX);
printf("%d\n",aa);
return 0;
}知道问题出在哪里吗?
发布于 2010-01-14 06:51:25
在C99之前的C中,给定作用域中的所有变量都必须在该作用域中的其他语句之前定义。虽然它最初看起来是一样的,但这与GMan的答案并不完全相同。特别是,函数可以包含定义其他作用域的其他块,这些块可以在外部块中的可执行语句之后定义变量:
int main() {
int x;
printf("whatever");
int y; // not allowed
{
int z; // allowed
}
return 0;
}虽然像这样定义一个块(与if语句或While循环等任何流控制无关)是相当不常见的,但它可以作为C语言的一部分。
https://stackoverflow.com/questions/2060855
复制相似问题