我用 C 编写了一个小程序来打印“Hello world”。我不是 C 程序员,但我喜欢尝试一下。我的程序中有一个错误。请告诉我这是什么? 这是我的程序:
int main(){
printf("Hello World");
}
我用我的 Java 经验写了这篇文章。我找不到问题所在。
最佳答案
您不能像 Java 中那样直接使用 printf() 函数。您应该告诉编译器您将使用输入/输出流。你可以在这一行中告诉它:
#include <stdio.h>
您还应该在源代码末尾输入此行:
return 0;
这将告诉编译器:
"If the program succeed It will return 0 otherwise It will return any other number"
这意味着如果你的程序成功,main()函数将返回0。然后编译就知道程序是好的。
最后你的完整代码是:
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}
要编译此文件并查看“Hello World”一词,只需将此文件另存为 .c 文件,然后在程序目录中打开 cmd 并键入
gcc hello.c -o hello && hello
(将“hello.c”替换为您的文件名,将“hello”替换为您要与 .exe 文件一起放置的名称)
记住我的电脑是Windows。这个编译代码是针对windows的。如果您的操作系统是类似 UNIX 的操作系统。然后使用此代码进行编译:
gcc hello.c -o hello
./hello
关于c - 如何用 C 语言编写 Hello World,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085251/