C Language 11th round: pre-processing command of the concentration camp

Source: Internet
Author: User

C language 11th round: pre-processing command of the concentration camp

"Learning goals"

1. Macro definition

2. The document includes "processing

3. Conditional compilation

Preprocessing commands: An environment that improves program design. Improve programming efficiency.

There are three main functions: macro definition, file inclusion, and file compilation.

ANSI standard-defined C Language preprocessing instruction preview table



A: macro Definition

( a ) macro definition without a number of parameters

Format: #define标识符 string

such as: #define PI 3.1415926

* Identifiers are called: macro names

* The process of replacing a macro name with a string at precompilation is: macro expansion.

* #define IS Macro definition command//Circle length # include <stdio.h> #define PI 3.1415926 int main (void) {         float R;  Radius         float C;  Circumference                 r= 3.0;                 c= pi* R;  Ask                 for the circumference long printf ("%.3f\n", c);  Keep 3 as Decimal                 


Ps:

(1) Macro names are generally uppercase letters, so that they are easy to be differentiated. But it can also be lowercase.

(2) Using Macro name substitution can reduce the amount of code. Easy to maintain

(3) The macro definition is not a C statement. So you don't have to add a semicolon at the end. If you add a semicolon, the macro name will replace the string and semicolon

#define NUM 123; There are semicolons. Compiler will error!!

(4) The scope of the macro definition can be aborted with the #undef command. Otherwise its scope is terminated by the beginning of the definition of this file.

#undef的使用 #include<stdio.h> int main (void) {         #define N-   //n replaces the                 printf ("%d\n", n);                 #undef   the scope of n//end N. Suppose there is no such statement suggesting that redefined                 #define N   //n instead                 of printf ("%d\n", n);            return 0; }


(5) When a macro is defined, it can refer to the defined macro name and can be replaced by layers

Such as:

#include <stdio.h> #define R 3.0#define pi3.1415926#define c2*pi*r#define spi*r*r int main (void) {         //print circle radius R. Perimeter c, area s         printf ("r=%.2f\n", R);         printf ("c=%.2f\n", C);         printf ("s=%.2f\n", S);                 return 0;}


(6) A character in a string enclosed in a "" (double apostrophe) in a program is not displaced even if it is the same as the macro name.

Such as:

#include <stdio.h> #define stringhello,world! #define STRING "hello,world!" int main (void) {         printf ("string") ;  string is not replaced with hello,world!                 Putchar (' \ n '); NewLine                   printf (STRING);    To be replaced by hello,world!                 return 0;}

Execution Result:

String

hello,world!

(7) A macro definition differs from the definition of a variable. Macro definitions only do character substitution, do not allocate storage space

( b ) macro definition with a number of parameters

Role: The substitution of the number of parameters

Format: #define宏名 (Tables) string

[1] Add a macro with a number of parameters

#include <stdio.h>//macros with parameters to replace # define ADD (A, B) ((a) + (b))//each variable using parentheses is to prevent the substitution of ambiguous int main (void) {         int n= 10;
   int m=;    int sum= 0;                 Sum= ADD (n, m);                 printf ("%d\n", sum);            return 0; }


[2] A macro substitution for sizeof with a number of parameters.

Able to find the length of the array ( Remember that the use of sizeof is limited !)

/* Instructions for use:    the strlen () function can only find the length of the string         SIZEOF cannot be used as the original size of the array to pass the parameters */#include <stdio.h>//define sizeof Macro # # sizeof ( Array) (sizeof (array)/sizeof (array[0])) int main (void) {         int num= 0;         int array[]= {1, 2,3, 3, 7, 5, 5, 6, 7, ten};          num= SIZEOF (array);         printf ("The length of the array is%d\n", num);            return 0; }

Execution Result:

The length of the array is 13

Ps:

(1) For a macro with a number of parameters, the displacement is done from left to right. Assume that the characters in the string are not a reference character (such as + in A + B) to keep.

(2) There is no space between the macro name of the macro definition and the parentheses of the parameter, otherwise it will be treated as part of the string

Note: The difference between a function and a macro

(1) The number of times a macro is used, the length of the program grows after the macro expands, and the function's call does not.

(2) The substitution of macros does not occupy the execution time. Only takes up compile time. The function takes the execution time (i.e. allocation unit, value pass, return, etc.)

B: files include " processing

"File includes" Processing: One source file can include all of the other source files.

Format: (a) #include "file name"

(b) #include < file name >

Differences between the two formats: use angle brackets to find in the Include files (that is, the library files) folder to look up (including folders are set by the user when setting the environment), but not in the source files folder to find, using the double-cited means first in the current source file folder, if not found to include folder (library file) to find.

Ps:

(1) An # include command can contain only one source file, multiple source files with multiple # include commands

(2) Assuming that file 1 to include file 2, and file 2 to include the file 3, you can have 1 # include in the file two contains the command to include the file 2 and the file 3, and the file 3 must be out of today's file 2 ago.

(3) Files include the ability to nest in use. That is, an include file can include a file that is included

(4) The included file (file2.h) and the file where it resides (that is, the source file with the # include command file2.c), after precompilation has become the same file (not two files). Therefore, assuming that there is a global static variable in file2.h, it is also valid in the File1.h file and does not have to be declared with extern.

C: Conditional Compilation

Conditional compilation: A condition that specifies compilation for part of the content, and that it satisfies certain conditions before compiling.

Format:

(1)

#ifdef identifiers

Procedure Section 1

#else

Procedure Section 2

#endif

Function: If the identifier is defined by a # define, the program Segment 1 is compiled, and the program segment 2 is compiled. Assuming there is no program segment 2, it is possible to omit #else.

#include <stdio.h> #define R3.0   //define R int main (void) {            #ifdef R  //similar to If-else                       printf ("r\n defined") ;                       #else                          printf ("r\n Not Defined");                       #endif  //Conditional inference end                       return 0;}

(2)

#ifndef identifiers

Procedure Section 1

#else

Procedure Section 2

#endif

Function: If the identifier is not defined by a # define, the program Segment 1 is compiled, and the program segment 2 is compiled instead.

(3)

#if constant expression

Procedure Section 1

#else

Procedure Section 2

#endif

Action: Assume that the value of the expression is true (that is, not 0). The program section 1 is compiled. Instead, the program segment 2 is compiled.

#include <stdio.h> #define R 3.0   //define R int main (void) {         #ifndefR  ///similar to If-else                 printf ("r\n Not defined") ;                 #else                    printf ("already defined r\n");                 #endif  //Conditional inference end                 Return0;}

"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^

When reproduced, keep the original link http://oursharingclub.joinbbs.net/ and http://blog.csdn.net/mirrorsbeyourself

C Language 11th round: pre-processing command of the concentration camp

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.