C language 15-preprocessing directives-macro definition

Source: Internet
Author: User
Tags pow

    • Introduction to preprocessing Directives
    • One, macro definition without parameters
    • Second, macro definition with parameters

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

Introduction to preprocessing Directives 1. C language before compiling the source program, will be some special pre-processing instructions to explain (such as the previous use of the # include file contains instructions), to produce a new source program (this process is called compilation preprocessing), Then do the usual compilation 2. In order to differentiate between preprocessing directives and generic C statements, all preprocessor directives begin with the sign "#" and End with no semicolon 3. The preprocessor directive can appear anywhere in the program, ranging from where it appears to the end of the file. It is customary for us to write pre-processing instructions at the beginning of the source program as much as possible, in which case it is scoped to the entire source program file 4. The pre-processing instructions provided by the C language include: macro Definitionfile containsconditional CompilationThis talk about macro definition, macro definition can be divided into 2 kinds: macro definition without parameters and macro definition with parameters. One, macro definition without parameters

1. General form

#define Macro Name string

such as # define ABC 10

The string on the right can also be omitted, such as # define ABC

1. Use

It is used to define constants by replacing all "macro names" in the source program with the right "strings" when compiling preprocessing.

Then write a program that calculates the perimeter based on the radius of the circle

1 #include <stdio.h> 2  3//The Macro name PI in the source program will be replaced by 3.14 when compiling preprocessing 4 #define PI 3.14 5  6//RADIUS calculation Perimeter 7 float according to circle radius Girth (float radius) {8     return 2 * PI *radius; 9}10 int main () {     float G = girth (2);     ("Perimeter:%f", g);     return 0;17}

A macro called PI is defined in line 4th, and after the pre-processing of the compilation, the 2 * PI *radius in line 8th becomes 2 * 3.14 * radius.

Output Result:

3. Use habits and attention

1> macro names are generally capitalized in order to distinguish them from variable names, but there is no syntax error in lowercase

2> the characters within the string that are enclosed in double quotation marks for the program, and does not replace the macro. Like what:

1 #define R 102 int Main () 3 {4     char *s = "Radio"; 5     return 0;6}

A macro called R is defined in line 1th, but ' R ' in line 4th is not replaced with 10. Radio

3> does not make a syntax check when it replaces a macro name with a string in the compilation preprocessing, but simply replaces the string. The source program that has expanded the macro name is checked for syntax only at compile time

1 #define I 1002 int Main () 3 {4     int i[3] = i;5     return 0;6}

At the time of the compilation preprocessing, the 4th line of I will be replaced with 100, regardless of the correct syntax. However, at the time of compiling, the 4th line of error will be reported.

The valid range of 4> macro names is from the defined position to the end of the file. If you need to terminate the scope of a macro definition, you can use the #undef command
1 #define PI 3.142/*3.4.5.6.7 */8 #undef PI

PI This macro is valid between lines 1th through 8th and is invalid after line 8th.

5> you can refer to a macro name that you have defined when defining a macro
#define R  3.0#define PI 3.14#define L  2*pi*r#define S  pi*r*r

Second, macro definition with parameters

1. General form

#define Macro Name (argument list) string

2. Role

When you compile preprocessing, replace all macro names in the source program with strings, and replace the arguments in the string with the parameters in the argument list on the right side of the macro name

1 #include <stdio.h> 2  3 #define AVERAGE (A, b) (A+B)/2 4  5 int main () 6 {7     int a = average (ten, 4); 8
   9     printf ("Average:%d", a);     0;11}

The 3rd line defines a macro average with 2 parameters, and the 7th line is actually replaced by: int a = (10 + 4)/2; the output is:

Does it feel like a function of this macro?

3. Use note

There must be no spaces between the 1> macro name and the argument list, otherwise all strings following the space are substituted strings

1 #define AVERAGE (A, b) (a+b)/22 3 int main () 4 {5     int a = average (ten, 4); 6     return 0;7}

Note the macro definition of line 1th, the macro name average with a space between (a, b), and the 5th line becomes this:

int a = (A, b) (A+B)/2 (10, 4);

This must have been compiled without a pass.

2> macros with parameters are expanded, only simple characters and parameters are replaced, and no calculations are performed. So when you define a macro, you typically enclose the string's arguments in a single parenthesis.

The following defines a macro D (a) that returns twice times the value of a:

    • If you define a macro without parentheses around the argument
1 #include <stdio.h> 2  3 #define D (a) 2*a 4  5 int main () 6 {7     int b = D (3+4); 8      9     printf ("%d", b);     return 0;11}

The 7th line will be replaced with int b = 2*3+4;, output result:

    • If you define a macro, enclose the argument in parentheses, and change the 3rd line above:
#define D (a) (a)

Note that the right side of a is enclosed in parentheses, and the 7th line is replaced with int b = (3+4), and the output is:

3> calculation results are best also enclosed in parentheses.

The following defines a macro p (a), which is the function of returning a squared:

    • If you don't enclose the calculation result with parentheses
1 #include <stdio.h> 2  3 #define POW (a) (a) * (a) 4  5 int main (int argc, const char * argv[]) {6     int b = POW/POW (2); 7      8     printf ("%d", b); 9     return 0;10}

Note that line 3rd does not use parentheses to extend the results of the calculation, only the parameters are enclosed. The 6th line of code is replaced by:

int B = (10) * (10)/(2) * (2);

After simplification: int b = 10 * (10/2) * 2; and the last variable B is:

    • If you enclose the result of the calculation in parentheses

Change the 3rd line of code above to:

#define POW (a) ((a) * (a))

Then the 6th line is replaced by:

int B = ((10) * (10))/((2) * (2));

After simplification: int b = (10 * 10)/(2 * 2), and final output:

This is the result we want.

This means that the preceding # define average (A, b) (A+B)/2 should be written in the # define average (A, B) (((a) + (b))/2).

5. Differences from functions

From the entire use of the process can be found that the macro definition with parameters, in the source program appears in the form and function very much like. But there is an essential difference between the two:

1> macro definition does not involve allocation of storage space, parameter type matching, parameter passing, return value problems

2> function calls execute while the program is running, and macro substitution occurs only during the compilation preprocessing phase. Therefore, the macro with the parameter has higher execution efficiency than the function.

C language 15-preprocessing directives-macro definition

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.