We have already created a C program, and then we analyze the code inside.
The project structure is as follows:
First, the Code analysis
Open the Main.c file in your project (the C program's source file extension is. c) and you can see that it is the only source file in the first C program, with the following code:
1 #include <stdio.h>2 3 int main (int argc, const char * argv[]) 4 {5 6 //Insert code here ... 7 printf ("Hello, world!\n"); 8 return 0;9}
1. #include <stdio.h>
- #include is one of the pre-processing directives for C, so-called preprocessing, which is done before the compilation, and the preprocessing directives are usually preceded by #
- After the #include instruction follows a file name, the preprocessor discovers the #include instruction, finds the file according to the filename, and includes the contents of the file in the current file. The text in the included file replaces the #include instruction in the source file as if you had copied all the contents of the contained file to the location of the #include instruction.
- If the included file extension is named. h, which we call "header file", the header file can be used to declare a function ("function" is an object-oriented "method"), in order to use these functions, you must first use the #include directive contains the header file where the function
- The #include directive is not limited to. h header files, and can contain any code files that the compiler can recognize, including. C,.hpp,.cpp, and so on, even. Txt,.abc and so on.
In other words, you can put the code from line 3rd to line 9th into another file, and then include it in a #include directive, such as:
1> put the code from line 3rd to line 9th into My.txt
2> include My.txt file in Main.c source file
The program can still run as usual, because the #include function is to completely copy the contents of the file to the location where the #include instruction is located
- But you may wonder why stdio.h use angle brackets to <>, while my.txt use double quotes ""? This is very good difference, if the system comes with the file, it is best to use <>; if it is the developer's own files created, it is best to use ""
Note: Here with TXT file purely demo, usually do projects do not do this, unless eat full of support, will write the code into TXT
2.main function
- As mentioned earlier, the functions in C are "methods" in object-oriented. C language is a process-oriented language, is a process-centric programming idea, is to analyze the steps needed to solve the problem, and then use the function of these steps step by step implementation, the use of a one-time call function can be
- There must be a main function in a C program, and there can be only one main function. The main function is the entry for the entire C program. The 3rd line of code in MAIN.C defines a main function.
- The return value of the main function is of type int, receiving 2 parameters, can actually not write parameters
The main function can be simplified into this:
Main () { //Insert code here ... printf ("Hello, world!\n");
return 0;}
Note here that the main function here does not write the return value type, does not mean that the function does not return a value, but rather that the return value type is type int, void represents the function does not return a value
Because the syntax limitations of the C language are not strict, we can further simplify the main function:
Main () { //Insert code here ... printf ("Hello, world!\n");}
Although it requires the main function to return a value of type int, we can not return
3.stdio.h
- stdio.h is a header file in the C language library that defines some standard input and output functions. In the 1th line of code in MAIN.C, the # include directive contains the Stdio.h file.
- The reason for the inclusion of the stdio.h file is that the printf function declared inside stdio.h is used in line 7th to output data to a standard output device, such as a display screen, and the text that is printed on the screen is the 7th line of code printf ("Hello, World!\n "), resulting in double quotation marks" "within the text of the C language string
back to topIi. steps for developing and running C programs
The step diagram is as follows:
1. Writing Procedures
The C language source file has the extension ". C", and the source file is stored in ASCII format and cannot be executed directly by the computer because the computer can only recognize binary instructions, that is, 0 and 1
2. Compiling (VC environment)
- The C source program is translated into a computer can recognize the binary form of the target code file, this process is called compilation, by the C compiler to complete
- While compiling, the syntax of the source program is also checked. If a syntax error occurs, the compilation fails. If the compilation succeeds, the target file is generated with the same name as the source program file with the extension ". obj". For example, MJ.C generates a target file after compiling mj.obj
- Each source file is compiled separately, and if there are multiple. C source files in a project, compiling successfully generates multiple corresponding. obj targets. In general, there is a correlation between the target files, such as a.obj may call a function defined in b.obj, so none of them can be executed by the computer alone, and the destination file does not contain the library functions that the program needs to run, etc.
3. Links (VC environment)
- The process of combining all the associated obj target files, together with the system-provided C library functions, to generate the executable file, called a "link"
- The file name of the executable file generated by the link is the same as the source program file with the extension ". exe" and the computer can execute directly
4. Running
* If you are in a Windows environment, simply double-click the ". exe" file to run the C language program
* Since our First C program is a command line project created with Xcode in Mac OS X system environment, Mac OS X system is based on UNIX system, so the resulting executable file is like this:
* Double-click to open with Terminal (command line):
back to topIii. Summary
Say so much, summarize the first C program running steps:
1. Execute the # include directive before compiling, copy the contents of the stdio.h into the source program
2. Compile the source program, generate the target file
3. Link the C-language function library, generate the executable file
4. Run the executable file and output "Hello, world!" on the screen
It may seem like a lot of steps, but when we click on the Xcode Run button, Xcode automatically executes the above 4 steps sequentially
"C Language" 03-the first C program code analysis