Cprogramming 简明教程

C - Hello World

每个渴望成为专业软件开发人员的学习者都从用他们正在学习的编程语言编写 Hello World 程序开始。在本章中,我们将学习如何在 C 语言中编写 Hello World 程序。

Every learner aspiring to become a professional software developer starts with writing a Hello World program in the programming language he/she is learning. In this chapter, we shall learn how to write a Hello World program in C language.

Hello World in C Language

在编写 Hello World 程序之前,请确保您的计算机中已设置好 C 编程环境。这包括 GCC 编译器、一个文本编辑器,最好还包括一个用于 C 编程的 IDE,例如 CodeBlocks。

Before writing the Hello World program, make sure that you have the C programming environment set up in your computer. This includes the GCC compiler, a text editor, and preferably an IDE for C programming such as CodeBlocks.

Example

第一步是为 Hello World 程序编写源代码。在您的计算机上打开一个文本编辑器。在 Windows 中,打开记事本或记事本++,输入以下代码并将其另存为 "hello.c"。

The first step is to write the source code for the Hello World program. Open a text editor on your computer. On Windows, open Notepad or Notepad++, enter the following code and save it as "hello.c".

#include <stdio.h>

int main(){

   /* my first program in C */
   printf("Hello World! \n");

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Hello World!

The Step-by-Step Execution of a C Program

让我们分步理解上述程序的工作方式。

Let us understand how the above program works in a step-by-step manner.

Step 1

上述代码中的第一个语句是 #include 语句,用于在当前 C 程序中导入 stdio.h 文件。这称为 preprocessor directive 。此头文件包含用于标准 IO 操作的几个库函数的定义。由于我们将调用 stdio.h 库中定义的 printf() 函数,因此我们需要在第一步中包括它。

The first statement in the above code is the #include statement that imports the stdio.h file in the current C program. This is called a preprocessor directive. This header file contains the definitions of several library functions used for stand IO operations. Since we shall be calling the printf() function which is defined in the stdio.h library, we need to include it in the first step.

Step 2

每个 C 程序都必须包含一个 main() function 。此程序中的 main() 函数在控制台终端上打印“Hello World”消息。

Every C program must contain a main() function. The main() function in this program prints the "Hello World" message on the console terminal.

在 main() 函数中,我们插入了一个 comment statement ,该注释最终会被编译器忽略;它是出于文档目的。

Inside the main() function, we have inserted a comment statement that is ultimately ignored by the compiler; it is for the documentation purpose.

下一条语句调用 printf() 函数。在 C 中,每个语句都必须以分号符号 (;) 结尾,否则编译器会报告错误。

The next statement calls the printf() function. In C, every statement must terminate with a semicolon symbol (;), failing which the compiler reports an error.

stdio.h 库文件中导入的 printf() 函数将 Hello World 字符串回显到标准输出流。对于 Windows,标准输出流是命令提示符终端,对于 Linux,它是 Linux 终端。

The printf() function, imported from the stdio.h library file, echoes the Hello World string to the standard output stream. In case of Windows, the standard output stream is the Command prompt terminal and in case of Linux it is the Linux terminal.

在 C 中,每个函数都需要有一个 return value 。如果函数不返回任何内容,则其值为 void 。在上面的示例中,main() 函数的返回值为 int 。由于 main() 函数不需要返回任何内容,因此将其定义为返回整数“0”。“return 0”语句还表示程序已成功编译和运行。

In C, every function needs to have a return value. If the function doesn’t return anything, its value is void. In the example above, the main() function has int as its return value. Since the main() function doesn’t need to return anything, it is defined to return an integer "0". The "return 0" statement also indicates that the program has been successfully compiled and run.

Step 3

接下来,我们需要从源代码 (“hello.c”) 编译和构建可执行文件。

Next, we need to compile and build the executable from the source code ("hello.c").

如果您使用的是 Windows,请在保存“hello.c”的文件夹中打开命令提示符。以下命令将编译源代码 −

If you are using Windows, open the command prompt in the folder in which "hello.c" has been saved. The following command compiles the source code −

gcc -c hello.c -o hello.o

-c 选项指定要编译的源代码文件。如果 C 程序没有任何错误,这将生成一个名为 hello.o 的对象文件。如果包含错误,则会显示这些错误。例如,如果我们忘记在 printf() 语句的结尾处加上分号,则编译结果将显示以下错误 −

The -c option specifies the source code file to be compiled. This will result in an object file with the name hello.o if the C program doesn’t have any errors. If it contains errors, they will be displayed. For example, if we forget to put the semicolon at the end of the printf() statement, the compilation result will show the following error −

helloworld.c: In function 'main':
helloworld.c:6:30: error: expected ';' before 'return'
   printf("Hello, World! \n")
                             ^
                             ;
helloworld.c:8:4:
   return 0;

要从已编译对象文件构建可执行文件,请使用以下命令 −

To build an executable from the compiled object file, use the following command −

gcc  -o hello.exe hello.o

hello.exe 现在已准备好从命令提示符运行,该命令提示符在终端中显示 Hello World 消息。

The hello.exe is now ready to be run from the command prompt that displays the Hello World message in the terminal.

C:\Users\user>hello
Hello World!

在 Ubuntu Linux 上,在运行对象文件之前,首先赋予其可执行权限,方法是在其前面加上“ ./ ”。

On Ubuntu Linux, the object file is first given executable permission before running it by prefixing "./" to it.

$ chmod a+x a.o
$ ./a.o

您还可以使用诸如 CodeBlocks 的 IDE 来更方便地输入代码、编辑、调试和运行 Hello World 程序。

You can also use an IDE such as CodeBlocks to enter the code, edit, debug and run the Hello World program more conveniently.

Using CodeBlocks IDE for C Programming

CodeBlocks 是用于 C/C++ 开发的最流行的 IDE 之一。如果您尚未安装,请安装它并打开它。从文件菜单中创建一个新文件,输入以下代码并将其保存为“hello.c”。

CodeBlocks is one the most popular IDEs for C/C++ development. Install it if you have not already done and open it. Create a new file from the File menu, enter the following code and save it as "hello.c".

Example

#include <stdio.h>

int main(){

   /* my first program in C */

   printf("Hello World! \n");

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Hello World!

如下所示,从构建菜单中选择构建并运行选项 −

Choose Build and Run option from the Build menu as shown below −

build menu

您也可以使用 F9 快捷键执行相同操作。如果程序没有错误,则构建日志选项卡显示以下消息 −

You can also use the F9 shortcut for the same. If the program is error-free, the Build Log tab shows the following messages −

gcc.exe   -c C:\Users\mlath\hello.c -o C:\Users\mlath\hello.o
gcc.exe  -o C:\Users\mlath\hello.exe C:\Users\mlath\hello.o
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Checking for existence: C:\Users\mlath \hello.exe
Executing: '"C:\Program Files\CodeBlocks/cb_console_runner.exe" "C:\Users\mlath\hello.exe"' (in 'C:\Users\mlath\Documents')

在单独的命令提示符窗口中,将显示输出 −

In a separate command prompt window, the output will be displayed −

Hello World!

Process returned 0 (0x0)   execution time : 0.236 s
Press any key to continue.

如果代码包含错误,则构建日志选项卡会回显这些错误。例如,如果我们在 printf() 语句中漏掉尾随分号,则日志如下 −

If the code contains errors, the build log tab echoes them. For instance, if we miss the trailing semicolon in the printf() statement, the log will be as below −

build messages

你可以使用任何其他 IDE 运行 C 程序。为此,你需要按照相应 IDE 的文档进行操作。

You can use any other IDE to run the C program. You will need to follow the documentation of the respective IDE for the purpose.

成功运行 Hello World 也确认 C 编程环境在你的计算机上运行正常。

Running the Hello World successfully also confirms that the C programming environment is working properly on your computer.