C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Analyze The First C Programming Language Program

Earlier, we gave a piece of the simplest C programming language code and demonstrated how to compile it under different platforms. In this section, we will analyze this code to give readers an overall understanding. code show as below:
#include <stdio.h>
int main()
{
    puts("iditect");
    return 0;
}

concept of function

Let's start with line 4, which prints "iditect" on the display. As we have said before, put ( ) after puts , and strings should also be placed in ( ) .

In the C programming language, some statements cannot be used with parentheses, and some statements must be used with parentheses. The one with parentheses is called Function.

C programming language provides many functions, such as input and output, obtaining date and time, file operations, etc., we only need a simple code to be able to use. However, the bottom layers of these functions are relatively complex, usually a combination of software and hardware, and many details and boundaries must be considered. If these functions are handed over to programmers to complete, it will greatly increase the learning cost of programmers and reduce the cost of learning. programming efficiency.

Fortunately, the developers of the C programming language have done a good thing for us. They have written a lot of code and completed the common basic functions, which we can use directly. But now the question is, how do you find what you need from so many codes? It is obviously very unwise to bring all the code in one go.

These codes have already been classified into different files, and each piece of code has a unique name. When using the code, just add ( ) after the corresponding name . Such a piece of code can independently complete a certain function and can be reused after being written once, which is called Function. Readers can think of a function as a piece of code that can be reused.

An obvious feature of a function is that it must be used with parentheses ( ) , and if necessary, the parentheses can also contain data to be processed. For example puts("iditect")Just use a piece of code with output function, the name of this code is puts, and "iditect" is the data to be processed by this code. Using functions has a professional name in programming, called Function Call.

If the function needs to process multiple data, separate them with commas , for example:
pow(10, 2);
This function is used to find the 2nd power of 10. It should be noted that functions in C programming language and functions in mathematics are not the same concept, so don't compare the two.

Custom function and main function

The functions that come with the C programming language are called Library Functions. Library is a basic concept in programming, which can be simply thought of as a collection of functions, often a folder on disk. The library that comes with the C programming language is called the Standard Library, and the library developed by other companies or individuals is called the Third-Party Library.

In addition to library functions, we can also write our own functions to extend the functionality of the program. Functions that you write yourself are called custom functions. Custom functions and library functions are written and used in exactly the same way, they are just written by different institutions.

Lines 2 to 6 in the example are a function we wrote ourselves. main is the name of the function, ( ) indicates that this is the function definition, and the code between { } is the function to be implemented by the function.

The function can receive the data to be processed, and can also tell us the processing result; use return to inform the processing result. The fifth line of code in the example shows that the processing result of the main function is the integer 0. return can be translated as "return", so the processing result of the function is called Return Value.

In line 2, int is shorthand for integer, meaning "integer". It tells us that the return value of the function is an integer.

Note that the custom function in the example must be named main. The C programming language stipulates that a program must have one and only one main function. main is called the main function and is the entry function of the program. When the program runs, it starts from the main function until the main function ends (the function ends when it encounters return or executes to the end of the function).

That is to say, without the main function, the program will not know where to start execution, and will report an error at runtime.

To sum up: Lines 2 to 6 define the main function main, whose return value is the integer 0, and the program will execute from here. The return value of the main function is received by the system at the end of the program execution. More about custom functions will be explained in detail in the chapter "C programming language functions", and will not be discussed here. In some textbooks, the main function is written as:
void main()
{
    // Some Code...
}
This can be compiled under VC6.0, but an error will be reported in GCC, because this is not the standard main function writing method, don't be misled, it is best to write it according to the format in the example.

The concept of header files

And one last question, what does #include <stdio.h> on line 1 of the example mean?

C programming language developers have written many common functions and put them in different files, which are called header files. Each header file contains several functions with similar functions. When calling a function, the corresponding header file must be imported, otherwise the compiler cannot find the function. In fact, the header file often only contains the description of the function, which tells us how to use the function, and the function itself is stored in other files and will be found when linking. For beginners, it can be temporarily understood that several functions are included in the header file.Use the #include command to import header files , and put the file name in < > , with or without spaces between #include and < >.

The header file is suffixed with .h , while the C programming language code file is suffixed with .c . They are both text files and there is no essential difference. The function of the #include command is only to copy the text in the header file to the current file. , and then compile with the current file. You can try to copy the contents of the header file to the current file, or not include the header file. The syntax rules for code in

.h are the same as in .c , you can also #include <xxx.c>, which is completely correct. However, no one will do this in actual development, which looks very unprofessional and non-standard.

The older C programming language standard library contains 15 header files, stdio.h and stdlib.h are the two most commonly used:
  • stdio is the abbreviation of standard input output, stdio.h is called "standard input and output file", and most of the functions contained are related to input and output, and puts() is one of them.
  • stdlib is the abbreviation of standard library, stdlib.h is called "standard library file", and the functions contained are relatively messy, mostly some general utility functions, system() is one of them.

final summary

Beginner programming, there are many basic concepts to understand, this section involves a lot, it is recommended that you read the above content several times, and you will gain something.

The example at the beginning of this section is the basic structure of a C programming language program. We might as well organize the ideas and analyze it as a whole:
1) Line 1 introduces the header file stdio.h, which is the most commonly used header file in programming . The header file does not have to be imported. We use the puts function, so we introduce stdio.h. For example the following code is completely correct:
int main()
{
    return 0;
}
We don't call any functions, so we don't have to include header files.

2) Line 2 begins to define the main function main. main is the entry function of the program, a C program must have a main function, and there can only be one.

3) Line 4 calls the puts function to output a string to the display.

4) Line 5 is the return value of the main function. The program runs correctly and generally returns 0.