First give you a simple example, let the reader have a holistic understanding, the code is as follows:
#include <stdio.h>
int main ()
{
puts ("cloud-dwelling community");
return 0;
}
The concept of a function
First look at the 4th line of code, which prints the "cloud community" on the monitor. As we have said before, the puts is followed by () and the string is also placed in ().
In the C language, some statements cannot be used with parentheses, and some statements must be parenthesized. Parentheses are called functions (function) .
C language provides a lot of functions, such as input and output, get date time, file operation, and so on, we only need a simple code to be able to use. But the bottom of these features are more complex, usually the combination of software and hardware, but also to consider a lot of details and boundaries, if these features are given to the programmer to complete, it will greatly increase the programmer's learning costs, reduce programming efficiency.
Fortunately, the C language developers have done a good deed for us, they have written a lot of code, the common basic functions are completed, we can use directly. But now the question is, so much code, how do you find out what you need from it? It is obviously unwise for a brain to get all the code.
The code is already categorized into separate files, and each piece of code has a unique name. When you use code, just add () it after the corresponding name. Such a piece of code can independently complete a function, once written to be reused after the completion, is called a function. the reader can assume that a function is a piece of code that can be reused.
A distinct feature of a function is that it must be used with parentheses () and, if necessary, include the data to be processed in parentheses. For example, puts ("cloud-dwelling community") uses a piece of code with output functionality, the name of which is puts, and the "cloud-dwelling community" is the data to be processed by this code. The use of functions in programming has a professional salutation, called function call.
If a function needs to work with multiple data, they are separated by commas, for example:
Pow (10, 2);
This function is used to find 10 of the 2-time square.
It should be noted that the C language function and mathematical functions are not the same concept, do not compare the two. The English name of the function is function, and it also means "functional". The mainland translates function into "functions", while Taiwan's translation is "functional", the reader should pay attention to distinguish.
Custom functions and main functions
The functions of the C language are called Library functions . A library is a basic concept in programming and can be simply considered a collection of column functions, often a folder on disk. The C-language library is called the Standard library (Standard library) , and other companies or individuals develop libraries called Third-party libraries (Third-party library) .
In addition to library functions, we can also write our own functions to expand the function of the program. The functions you write are called custom functions. Custom functions and library functions are written and used exactly the same way, but are written by different organizations.
The 2nd to 6th line of code in the example is a function we wrote ourselves. Main is the name of the function, which indicates that this is the function definition, and that the code between {} is the function to implement.
The function can receive the data to be processed, and it can also tell us the result of the process; The 5th line of code in the example shows that the main function's processing result is an integer 0. Return can be translated as "back", so the processing result of the function is called the return value.
In line 2nd, int is shorthand for integer, meaning "integer". It tells us that the return value of the function is an integer.
It is important to note that the custom function in the example must be named Main. C language stipulates that a program must have and only one main function. Main is called the main function , is the entry function of the program, the program runs from the main function, until the main function ends (when you encounter return or execution to the end of the function, the function ends).
That is, no main function program will know where to start executing, and the runtime will make an error.
To sum up: the 2nd to 6th line of code defines the main function main, whose return value is an integer 0, from which the program will begin execution. The return value of the main function is received by the system at the end of the program run.
More about custom functions, which we'll cover in more detail in the C language function chapter, is no longer discussed here.
Some textbooks will write the main function:
void Main ()
{
//Some Code ...
}
This can be compiled under VC6.0, but in C-free, GCC will be the error, because this is not the standard main function of the writing, we do not be misled, it is best to follow the example in the format to write.
The concept of a header file
And one last question, what does the #include <stdio.h> in the 1th line of the example mean?
C-language developers write a number of commonly used functions and place them in separate files, called headers (header file ) . Each header file contains several functions that are similar, and when a function is invoked, the corresponding header file is introduced, otherwise the compiler cannot find the function.
In fact, headers often contain only the description of the function, which tells us how the function is used, and the function itself is saved in other files and is found in the link. For beginners, it can be tentatively understood that the header file contains several functions.
The header file is introduced using the #include command, and the file name is placed in < >, and there can be spaces between #include and < >, or None.
The header file is a . h suffix, and the C language code file is a. c suffix, they are all text files, there is no essential difference, #include command is simply 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, as well as not to introduce header files.
The syntax rules for code in . h are the same as in . C , and you can #include <xxx.c>, which is exactly right. But no one in the actual development will do so, it looks very unprofessional, and not standard.
The earlier C language standard library contains 15 header files, and stdio.h and stdlib.h are the most commonly used two:
Stdio is the abbreviation of standard input ouput, stdio.h is referred to as "standard input and Output file", contains functions mostly related to input and output, puts () is one of them.
Stdlib is the abbreviation of standard library, Stdlib.h is called "Standard library file", contains the function is more messy, many are common tool function, System () is one of them.
The final summary
Beginners programming, there are a lot of basic concepts need to understand, this section involves a lot of suggestions that the above content read more than a few times, will be harvested.
The example at the beginning of this section is the basic structure of a C language program, we might as well tidy up the idea, from the overall analysis again:
1 The 1th line introduces the header file Stdio.h, which is the most commonly used header file in programming. The header file is not to be introduced, we use the puts function, so we introduce the stdio.h. For example, the following code is completely correct:
We don't call any functions, so we don't have to introduce header files.
2) line 2nd begins to define main function. Main is the entry function of the program, a C program must have a main function, and only one.
3) Line 4th calls the puts function to output the string to the monitor.
4) Line 5th is the return value of the main function. The program runs correctly generally returns 0.
The above is the C language Simple program analysis, hoping to help beginners.