Study Notes 03-first C program code analysis

Source: Internet
Author: User
I. Code Analysis

Open the main. c file in the project (the source file extension of the C program is. C), and you can find that it is the only source file in the first C program. The Code is as follows:

 

1 #include <stdio.h>2 3 int main(int argc, const char * argv[]) {4     // insert code here...5     printf("Hello, World!\n");6     return 0;7 }

 

1. # include <stdio. h>
  • # Include is one of the pre-processing commands in C language. The so-called pre-processing is the processing done before compilation. Pre-processing commands generally start #.
  • # The include command is followed by a file name. After the Preprocessor finds the # include command, it searches for the file based on the file name and includes the file content to the current file. The text in the contained file will replace the # include command in the source file, just as you copy all the content in the contained file to the location where the # include command is located.
  • If the extended name of the included file is. h, which is called "header file". The header file can be used to declare functions ("functions" are "methods" in Object-Oriented Systems). To use these functions, you must first use the # include command to include the header file of the function.
  • # The include command is not only limited to. h header files, but can contain any C/c00000000code file that can be recognized by the compiler, such as. C,. C,. hpp,. cpp, and. txt or. ABC.
  • If it is a file that comes with the system, it is best to use <>; if it is a file created by the developer, it is best to use ""
2. Main Function
  • As mentioned above, the function in C is the "Method" in the object-oriented model ". C language is a process-oriented language. It is a process-centric programming idea. It first analyzes the steps required to solve the problem, and then uses functions to implement these steps step by step, you can call functions one by one.
  • A c program must have one main function and only one main function. The main function is the entrance of the entire C program. The first line of code in Main. c defines a main function.
  • The Return Value of the main function is of the int type. If two parameters are received, no parameter can be written.

The main function can be simplified as follows:

 

1 main()2 {3     // insert code here...4     printf("Hello, World!\n");5     return 0;6 }

 

Note: The main function does not write the return value type. It does not mean that the function does not return the value. Instead, it indicates that the return value type is int. Void indicates that the function does not return the value.

Because the syntax constraints of C language are not strict, we can further simplify the main function:

 

1 main()2 {3     // insert code here...4     printf("Hello, World!\n");5 }

 

Although it requires the main function to return the int type value, we can not return

 

 

3. stdio. h
  • Stdio. H is a header file in the C language function library, which defines some standard input and output functions. In the 1st line of Main. C code, the # include command contains the stdio. h file.
  • Here stdio is included. h file, because it is used in stdio in row 5th. h internally declares the printf function, which can output data to the standard output device (such as the display screen). The output text on the screen is the 5th lines of code printf ("Hello, world! \ N "). The text in double quotation marks" "is a string of C language.

2. Steps for developing and running C Programs

The steps are as follows:

1. Write a program

The C language source file extension is ". c". The source file is stored in ASCII code format and cannot be directly executed by the computer, because the computer can only recognize binary commands, that is, 0 and 1.

2. Compile (in the VC environment)
  • Translate the C source program into a binary object that can be recognized by a computer. This process is called compilation and completed by the C compiler.
  • At the same time of compilation, the source program syntax is also checked. If a syntax error occurs, the compilation fails. If the compilation succeeds, the target file is generated. The target file name is the same as the source file name, and the extension is ". OBJ ". For example, MJ. C is compiled to generate the target file MJ. obj.
  • Each source file is compiled independently. If a project contains multiple. C source files, multiple. OBJ targets are generated after compilation. In general, the target file is associated, such as. OBJ may call B. OBJ. Therefore, they cannot be executed independently by computers, and the target file does not contain the library functions required for running the program.
3. Link (in VC environment)
  • The process of combining all associated OBJ target files and C library functions provided by the system to generate executable files is called "Link"
  • The file name of the executable file generated by the link is the same as that of the source program file, and the extension is ". EXE". The computer can directly execute
4. Run

* In Windows, double-click the ". EXE" file to run the C language program.

* Because our first C program is a command line project created using xcode in Mac OS x, Mac OS X is based on UNIX, the generated executable file is as follows:


* Double-click a terminal (command line) to open it:

Iii. Summary

Summarize the procedure for running the first C program:

 

1 #include <stdio.h>2 3 int main()4 {5 6     // insert code here...7     printf("Hello, World!\n");8     return 0;9 }

 

1. Run the # include command before compilation to copy the content of stdio. H to the source program.

2. Compile the source program and generate the target file.

3. link the C language function library to generate executable files

4. Run the executable file and output "Hello, world!" on the screen! "

When you click the run button of xcode, xcode automatically performs the preceding four steps in sequence.

 

Study Notes 03-first C program code analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.