简体   繁体   English

如何用 C 编写一个 Hello World

[英]How to write a Hello World in C

I wrote little program to print "Hello world" in C. I'm not a C programmer, but I liked to try it.我写了一个小程序来用 C 打印“Hello world”。我不是 C 程序员,但我喜欢尝试。 In my program there is an error.在我的程序中有一个错误。 Please tell me what is it?请告诉我是什么? This is my program:这是我的程序:

int main(){
    printf("Hello World");
}

I wrote this with my Java Experiences.我用我的 Java 经验写了这个。 I can't find what is wrong.我找不到什么问题。

You can't directly use printf() function as in Java.您不能像在 Java 中那样直接使用 printf() 函数。 You should tell the compiler that you are going to use the input/output stream.您应该告诉编译器您将使用输入/输出流。 You can tell it in this line:您可以在这一行中说明:

#include <stdio.h>

and also you should enter this line at the end of the source code:并且您应该在源代码的末尾输入这一行:

return 0;

this will tell the compiler :这将告诉编译器:

"If the program succeed It will return 0 otherwise It will return any other number" “如果程序成功,它将返回 0 否则它将返回任何其他数字”

This means if your program is successful main() function will return 0. Then the compile know the program is Ok.这意味着如果你的程序成功 main() 函数将返回 0。然后编译知道程序是好的。

Then at last your complete code is:那么最后你的完整代码是:

#include <stdio.h>

int main() {
    printf("Hello world");
    return 0;
}

To compile this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type要编译它并看到“Hello World”这个词,只需将此文件另存为 .c 文件并在程序目录中打开 cmd 并键入

gcc hello.c -o hello && hello

(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file) (将“hello.c”替换为您的文件名,将“hello”替换为您要放在 .exe 文件中的名称)

Remember My computer is Windows.记住我的电脑是Windows。 And this compile code is for windows.这个编译代码是针对windows的。 If your OS is UNIX like OS.如果您的操作系统是类似 UNIX 的操作系统。 then use this code to compile:然后使用此代码进行编译:

gcc hello.c -o hello
./hello

A full hello world program in C:一个完整的 C 语言的 hello world 程序:

#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}

Then compile (assuming gcc) and execute it:然后编译(假设gcc)并执行:

gcc -o test test.c
./test

First, you have to use a header file.首先,您必须使用头文件。

#include <stdio.h>

What that does is bring up a header file with a bunch of commands in them.这样做是调出一个头文件,其中包含一堆命令。 That will make it recognize the "printf" piece of code.这将使其识别“printf”代码段。 Next, you have to close your program.接下来,您必须关闭您的程序。 By not having a closing statement, the program will not compile because it doesn't know if that is the end of the code.由于没有结束语句,程序将无法编译,因为它不知道这是否是代码的结尾。 Use this at the end of your program...在程序结束时使用它...

return 0;

That closes the program, meaning that the compiler can stop looking for other code.这将关闭程序,这意味着编译器可以停止寻找其他代码。 You may also want to look at some programming manuals (there are dozens of free online ones) to learn about the syntax.您可能还想查看一些编程手册(有许多免费的在线手册)来了解语法。

One last thing: Most pieces of C code require a semicolon at the end.最后一件事:大多数 C 代码片段都需要在末尾使用分号。 This is not true for the "int main" statement, nor is it for the header file which I have defined above.这不适用于“int main”语句,也不适用于我上面定义的头文件。 The "return" function that closes the program, does however, need a semicolon.然而,关闭程序的“返回”函数确实需要一个分号。

Hoped this helped.希望这有帮助。

Should also include a pause at the end:最后还应该包括一个停顿:

#include <stdio.h>

int main(void) {
    printf("Hello World\n");

    //Read a character from the console
    getchar();

    return 0;
}

Just like import in Java programs, in here you have to include libraries you're using in your program.就像在 Java 程序中导入一样,在这里您必须include您在程序中使用的库。 You have used library function printf , but not included stdio.h .您使用了库函数printf ,但未包含stdio.h

I agree there are many ways to write one of the simplest way is我同意有很多方法可以编写其中一种最简单的方法是

#include<stdio.h>

int main(void){
  printf("Hello World\n");
  return 0;
}

You can even use different ways as suggested above.您甚至可以使用上述建议的不同方式。

You should first look at the structure of "main".您应该首先查看“main”的结构。 Try to understand the various parts as already explained so well in the above answers.尝试理解上面答案中已经解释得很好的各个部分。

"#include" : The preprocessing directives to be included in the program. "#include" :要包含在程序中的预处理指令。 But why?但为什么? Because you are trying to use the functions defined inside them.因为您正在尝试使用其中定义的函数。

int : The return type of "main" program. int :“main”程序的返回类型。 But why?但为什么? Because the function calling "main" needs to know if the "main" program has functioned correctly.因为调用“main”的函数需要知道“main”程序是否运行正常。

main : The entry point of your code. main :代码的入口点。 Dont ask why here :-)不要在这里问为什么:-)

main( void ) : To tell the compiler that we are not passing any arguments to program "main" main( void ) :告诉编译器我们没有向程序“main”传递任何参数

return 0 : Beacuse you promised "main" that you will return something if "main" will function properly. return 0 :因为你承诺“main”,如果“main”正常运行,你会返回一些东西。

Finally the code:最后是代码:

#include <stdio.h>
int main( void )
{
    printf( "Hello World\n" ) ; //Notice the '\n' here. Good coding practice.
    return 0 ;
}
 #include <stdio.h>                   //Pre-processor commands<br/>
 void main()                          //Starting point of the program<br/>{ //Opening Braces            
     printf("Hello World\n");         //Print Hello World on the screen<br/> 
     return 0;
 }                                    //Ending Braces

Check it once it will work, I have written it with comments:一旦它可以工作,请检查它,我已经写了评论:

#include<stdio.h>               //Pre-processor commands

void main() {
    printf("Hello World\n");    //Print Hello World on the screen
}

A full hello world program in C:一个完整的 C 语言的 hello world 程序:

#include <stdio.h>

int main(void) {
    printf("Hello World\n");
    return 0;
}
Then compile (assuming gcc) and execute it:

gcc -o test test.c
./test

You can't use printf() function as in Java.您不能像在 Java 中那样使用 printf() 函数。 You have to tell the compiler what you are going to use.你必须告诉编译器你要使用什么。 You can tell this as follows:-你可以这样说:-

#include <stdio.h>

You must enter this line in last:-您必须在最后输入此行:-

return 0;

Then Your complete code is:-那么你的完整代码是:-

#include <stdio.h>
int main(){
    printf("Hello World");
return 0;
}

For compiling this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type:-要编译它并看到“Hello World”这个词,只需将此文件另存为 .c 文件并在您的程序目录中打开 cmd 并键入:-

gcc hello.c -o hello && hello

(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file) (将“hello.c”替换为您的文件名,将“hello”替换为您要放在 .exe 文件中的名称)

Remember My computer is Windows.记住我的电脑是Windows。 So I can compile only for Windows OS.所以我只能为 Windows 操作系统编译。

#include <stdio.h>
int main() {

    // printf, used to print (display) Hello World
    printf("Hello World ! ");

    // return 0, as the main function is of type int so it must return an integer value
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM