1 /*The first C language program2 print Hello, world*/3#include <stdio.h>//contains header files Stdio.h4 intMain () {//Main function5printf"Hello, word\n.");//Print a string6 return 0;//returns 0, indicating that the program is running correctly7}
Although the program is simple, it can still be a big hurdle for beginners, because to do this, you first write code, compile, link, and run, and finally see the output. With these operational details in hand, other things are easier.
About compiling and linking is explained in the next section, where the program is interpreted first:
- Line 3rd contains the standard library file, which is called a file containing command, and a file with an. h extension is called a header file.
- Line 4th defines a function named Main, which does not accept parameter values; The statements of the main function are enclosed in curly braces; int is the main function that returns the value type.
- Line 5th Prints "Hello, World", and the main function calls the library function printf to display the sequence of characters.
- Line 6th indicates that the return value of the main function is 0,return let the function return a value.
- The 7th line ends the main function, and the curly brackets must appear in pairs.
The contents of "/* * * *" and "//" are commented, which are used to describe the program, and comments are automatically ignored at compile time.
A C language program, regardless of its size, is made up of functions and variables.
A function has certain functions that can perform certain actions, and the function contains statements to illustrate the process of operation. Variables are used to store the values used during the calculation.
In this case, the name of the function is main. In general, there is no limit to the naming of functions, but main is a special function name, and each program executes from the beginning of the main function, which means that each program must contain a main function at a location.
The main function usually calls other functions to help with some work, and the function that is called can be written by ourselves or from the function library. The first line of the above section of the statement # include <stdio.h> is used to tell the compiler to include the standard input/output library in this program. Many C language source programs contain this line of statements at the beginning of the program. We will cover the standard library in detail in subsequent chapters.
One way to exchange data between functions is to call a function to provide a list of values (called parameters) to the called function. A pair of parentheses following the function name surrounds the argument list. In this case, the main function does not require any parameters, so it is represented by an empty parameter table ().
The statements in the function are enclosed in a pair of curly braces {}. The main function in this example contains the following two statements:
printf ("Hello, word\n"); return 0;
When calling a function, you only need to use the function name plus the parameter table enclosed in parentheses. The above statement calls the printf function as a parameter called "Hello, world\n." printf is a library function for printing output, where it prints a string in the middle of a double quotation mark.
A sequence of characters enclosed in double quotation marks is called a string or string constant, such as "Hello, world\n" is a string. Currently we only use strings as parameters for printf and other functions.
In the C language, the character sequence \ n denotes a line break, and when it is encountered in print, the output print wraps, starting at the beginning of the left end of the next line. If you remove \ n from the string (this is a worthwhile exercise), the output will not wrap even after the print is finished. In the parameters of the printf function, you can only use \ n to represent a newline character. If you replace a program with a line break, for example:
printf ("Hello, Word
");
The C compiler will produce an error message.
The printf function never wraps, so we can call the function multiple times to get a long output line in stages. The first program given above can also be changed to the following form:
#include <stdio.h>int main () { printf ("Hello,"); printf ("word"); printf ("\ n"); return 0;}
This program is the same as the output from the previous program.
The first C language program