好吧,所以我在C中编写了这个编程工作,我一直在使用Pelles C,到目前为止一切都在膨胀,但最终它需要在大学的Unix系统上运行.所以我尝试在他们的Unix上编译,然后我突然得到一堆以前不存在的错误.
所以我要说这是一个主要的功能:
#include <stdio.h>
int main (int argc, char* argv[]){
printf("Hello World");
int anInt;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我给出了编译命令......
cc main.c
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
"main.c",第5行:语法错误之前或之后:int
...这是其中一个例子,在互联网上有一个Unix命令的常见例子,但它实际上并不是你曾经使用过的那个?或者Pelles C在这里为我"填补空白"?或者只是那个编译器,或什么?
我以前没见过这个.当我在我的Ubuntu服务器上尝试它时,你的源代码编译得很好.但是,当我尝试使用该-pedantic标志时,我收到以下错误:
hello.c: In function ‘main’:
hello.c:5:5: warning: ISO C90 forbids mixed declarations and code [-Wpedantic]
int anInt;
^
Run Code Online (Sandbox Code Playgroud)
因此,您的解决方案是找到一个支持C90之后的标准的编译器,或者更改源代码以在代码之前移动声明:
#include <stdio.h>
int main (int argc, char* argv[]){
int anInt;
printf("Hello World");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更准确地说,变量必须在它们作用域的块中的代码之前声明,因此这也是有效的:
#include <stdio.h>
int main (int argc, char* argv[]){
printf("Hello ");
{
int anInt;
printf(" World");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)