May 11, 2021 C
Before we learn the basic building blocks of the C language, let's look at a minimal C program structure, which we can use as a reference in the next chapter.
C The program consists mainly of the following sections:
Let's look at a simple piece of code that outputs the
Hello W3Cschool!
:
#include <stdio.h>
int main()
{
/* 我的第一个 C 程序 */
printf("Hello, W3Cschool! \n");
return 0;
}
Let's take a look at the above procedure:
#include <stdio.h>
preprocessor instruction that tells the C compiler to include the
stdio.h
file before actually compiling.
int main()
the main function, and the program starts here.
/*...*/
They are called comments for programs.
printf(...)
another function available in C that displays the message
Hello, W3Cschool!
return 0;
Terminates
main()
function and returns a value
0
Let's take a look at how to save the source code in a file and how to compile and run it. Here are the simple steps:
hello.c
gcc hello.c
the carriage return, and compile the code.
a.out
(in linux operating
)
executable file.
a.out
execute the program.
Hello W3Cschool
screen.
$ gcc hello.c
$ ./a.out
Hello, W3Cschool!
Make sure that the gcc compiler is included in your path and that you run
hello.c
source file hello.
If it is the source file for more than one c code, compile as follows:
$ gcc test1.c test2.c -o main.out
$ ./main.out