c language macros, conditional compilation

Finishing notes:

C language macro definition with parameters

 

Macro definition

The general form of the macro is defined as:
#define macro name string or expression
The preprocessing is to replace, the macro name is replaced with the corresponding character string expression.

Macro definition string:

#define macro-name string

In this example the statement int sum = 20 + N;, N was replaced by 100

#include <stdio.h>
#define N 100
int main(){
    int sum = 20 + N;
    printf("%d\n", sum);
    return 0;
}

Macro definitions expression:

#define   macro name (expression)
#include <stdio.h>
#define M (n*n+3*n)
int main(){
    int sum, n;
    printf("Input a number: ");
    scanf("%d", &n);
    sum = 3*M+4*M+5*M;
    printf("sum=%d\n", sum);
    return 0;
}

Note: The macro definition must be an expression in parentheses

Some explanations for macro definitions

1) macro definition macro name is a character string to indicate, to the time and expansion of a macro substituted macro name string, this is just a simple and crude replacement. String can contain any character, it can be a constant expression, IF statements, functions, etc., it does not make any preprocessor checks, if wrong, only found in the source program has been compiled macro expansion.

2) are not described or macro definition statement, end of the line do not have a semicolon, such as with semicolon even be replaced with a semicolon.

3) macro definition must be written outside the function, its scope is defined in the macro command plays the source end. To terminate the use #undef scope command. For example:
it represents only PI () function efficiently in main, invalid FUNC () in.

#define PI 3.14159
int main(){
    // Code
    return 0;
}
#undef PI
void func(){
    // Code
}

4) If the code is surrounded by quotation marks macro name, the macro preprocessor not made thereto instead.

5) allows nested macro definitions, macro names may be used in the character string defined in the macro definition, when expanded by the substitution in the layers of the macro preprocessor. E.g:

#define PI 3.1415926
 #define S * Y Y * PI / PI * macro name is defined * / 
for statements: 
the printf ( " % F " , S); 
after the macro substitution becomes: 
the printf ( " % F " , 3.1415926 * Y * Y);

6) customary macro name in uppercase letters, so the difference in the variables. But also allows lowercase letters.

7) data indicating the type of available macros, the writing easy. E.g:

#define UINT unsigned int

UINT available in the program as variable declarations:

UINT a, b;

It is noted that data indicating the type definition data and the difference between using typedef specifier using macro definitions. String macro definition simply replaced by a pre-processor to process; and typedef is processed during compilation by a compiler, it is not simple string replacement, and data type to the original starting a new name, it as a new data type.


Macro definition with parameters


C language allows macros with arguments. In the macro definition parameters are called "formal parameters" parameter in the macro call is called "actual parameters", and this is somewhat similar function.

Macro arguments, not only during deployment to replace the string, but also to replace a parameter arguments.

The general form of parameterized macro definition as:
#define macro name (parameter list), or string expression

May be contained in the string each parameter, parameter parameter list separated by commas.

The general form with reference to the macro call:
macro name (argument list);
for example:

#define M (Y) + Y * Y * Y. 3 // macros 
// Code 
K M = ( . 5 ); // macro call

 

note:

#include <stdio.h>
#define SQ(y) (y)*(y)
int main(){
    int a, sq;
    printf("input a number: ");
    scanf("%d", &a);
    sq = SQ(a+1);
    printf("sq=%d\n", sq);
    return 0;
}
sq=(a+1)*(a+1);

result:

input a number: 9
sq=100

#include <stdio.h>
#define SQ(y) y*y
int main(){
    int a, sq;
    printf("input a number: ");
    scanf("%d", &a);
    sq = SQ(a+1);
    printf("sq=%d\n", sq);
    return 0;
}
sq=a+1*a+1;

result:

input a number: 9
sq=19

 

Conditional compilation

⑴ # if ... # else ... format

 

#if determination condition 
           code segments. 1 
#else 
           code segment 2 
#endif 
or 

#if determination condition 1 
           1 Code segment 
#elif determination condition 2 
           code segment 2 
#else 
           code segment. 3 
#endif

Function: and if ... else ... expression is the same. Suitable scene determining the presence of true and false, this condition is a general expression.


⑵ # ifdef ... # else ... or # ifndef ... # else ... format

#ifdef identifier 
           code segment 1 
#else 
           code segment 2 
#endif 

or 

#ifndef identifier 
           a code segment 
#else 
           code segments 2 
#endif

Features: The main condition is judged to see if the identifier is defined (#define definitions).

In reality, the project will use a lot of conditional compilation. For example, use conditional compilation by various hardware platforms; to achieve platform and product line management through conditional compilation; to distinguish between the official version and debug version by conditional compilation, and so on.

 

Instruction preprocessing portion

Instructions
# dummy instruction, no effect
#include comprising a source code file
#define for macros
#undef cancel the macro defined
#if if the given condition is true, the compiler code below
#ifdef If the macro has been defined, the compiler below Code
#ifndef If the macro is not defined, the compiler code below
#elif #if given if the previous condition is not true, the current condition is true, the compiler code below
#endif end of a # if ...... # else conditional compilation block

 

Guess you like

Origin www.cnblogs.com/-wenli/p/12459416.html