[C Language] 03-analysis of the first C program code

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

In the previous article, we have created a C program and analyzed the code.

The project structure is as follows:

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 {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 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.

That is to say, you can put the code from lines 3rd to lines 9th into other files, and then include it with the # include command, for example:

1> Add the code from row 3 to row 9to my.txt.

2> include the my.txt file in the main.c source file

The program can still run as usual, because the # include function is to completely copy the file content to the location where the # include command is located.

  • However, you may wonder why stdio. h uses angle brackets <> and my.txt uses double quotation marks ""? This is a good difference. It is best to use the files that come with the System <>; if it is a file created by the developer, it is best to use ""

Note: The txt file is used for demonstration. It is not used for projects at ordinary times. The Code will be written to the txt file unless you are full of support.

 

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:

main(){    // insert code here...    printf("Hello, World!\n");
return 0;}

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:

main(){    // insert code here...    printf("Hello, World!\n");}

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 7th. 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 7th lines of code printf ("Hello, World! \ N "); the text in the double quotation mark" "is a C-language string.

 

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

To sum up the running steps of the first C program:

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! "

It seems complicated and complicated to use multiple steps. However, when we click the Xcode running button, Xcode automatically performs the above four steps in sequence.

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.