C Language Learning Tutorial Nineth Chapter-pretreatment (4)

Source: Internet
Author: User

2. The second form of:
#ifndef identifier
Program Section 1
#else
Program Section 2
#endif
The difference with the first form is to change "ifdef" to "ifndef". Its function is to compile the program segment 1 if the identifier has not been defined by the #define command, otherwise the program section 2 is compiled. This is the reverse of the first form of functionality.

3. Third form:
#if constant expression
Program Section 1
#else
Program Section 2
#endif
It functions as if the value of the usual expression is true (not 0), the program Segment 1 is compiled, or the program segment 2 is compiled. So the program can perform different functions under different conditions.
#define R 1
Main () {
float c,r,s;
printf ("Input a number:");
scanf ("%f", &c);
#if R
R=3.14159*c*c;
printf ("Area of round is:%f\n", R);
#else
S=c*c;
printf ("Area of the square is:%f\n", s);
#endif
}
In this case, a third form of conditional compilation is used. In the first line of the program macro definition, define R to 1, so when conditional compilation, the value of the constant expression is true, so the circle area is computed and output. The conditional compilation described above can certainly be implemented with conditional statements. However, using conditional statements will compile the entire source program, the generated target code program is very long, and conditional compilation, then only compile the program section 1 or 2 of the program, according to the conditions, the resulting target program is shorter. Conditional compilation is necessary if the program segment of the condition selection is very long.

Summary of this chapter
1. The preprocessing function is a special function of C language, which is completed by the preprocessor before the source program is formally compiled. Programmers use preprocessing commands to invoke these functions in a program.

2. A macro definition uses an identifier to represent a string that can be a constant, a variable, or an expression. This string is substituted for the macro name in a macro call.

3. A macro definition can have parameters, and when a macro is invoked, the parameter is substituted with an argument. Rather than "value transfer."

4. To avoid errors when macro substitution occurs, the string in the macro definition should be bracketed, and the form arguments appearing in the string should also be bracketed.

5. File inclusion is an important feature of preprocessing, which can be used to connect multiple source files into a single source file for compilation, resulting in the creation of a target file.


6. Conditional compilation allows you to compile only the program segments in the source program that meet the conditions so that the resulting target program is shorter, thereby reducing memory overhead and increasing the efficiency of the program.

7. The use of preprocessing functions to facilitate the revision of the program, reading, porting and debugging, but also facilitate the implementation of modular programming.

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.