Linux C preprocessor commands

preprocessing command

1. Macro definition

The C language standard allows the use of an identifier to represent a string in a program, called a macro. The identifier is the macro name. When compiling and preprocessing, all macro names in the program are replaced with corresponding strings. This process is called macro substitution. There are two types of macros: macros without parameters and macros with parameters.

1. Macro without parameters

The general form of a parameterless macro definition is: #define identifier string

"#" means that this line is a compilation preprocessing command. define is the keyword of the macro definition, and the identifier is the macro name. The string is the content replaced by the macro name, which can be a constant, an expression, etc.

E.g:


2. Macro definition with parameters

A macro definition with parameters is similar to a function with parameters, and the general form of its definition is:

#define Identifier (parameter list) string.

E.g:



2. The file contains

File inclusion refers to including the entire contents of the specified source file into the current source program file.

The general form of a file containment command is:

#include "filename" or #include <filename>

3. Conditional compilation

Conditional compilation means that under certain conditions, the conditions are met and those that are not met are processed separately - some statements are compiled when the conditions are met, and other statements are compiled when the conditions are not met.

Conditional compilation commands have the following modes.

Mode one:

#ifndef Identifier

Block 1:

#endif

The meaning is: if no identifier is defined, then compile segment 1.

The program segment 1 here can be either a statement group or an execution command.

E.g:

#ifndef getkey;
#define getkey;
#include<sys\types.h>
#endif;

The meaning of the code is: if the symbolic constant getkey is not defined, the output is defined and the header file sys/types.h is included.

Mode two:

#ifndef Identifier

block 1

#else

block 2

#endif

Its meaning is: if no identifier is defined, compile program segment 1, otherwise, compile program segment 2.

Mode three:

#ifdef identifier

block 1

#endif

The meaning is: if the identifier is defined, program segment 1 will be compiled, otherwise, the program segment will not be compiled.

E.g:

#ifdef DEBUG
printf("a=%d,b=%d",a,b);
#healthiness

When calling a program, you can add the following statement at the beginning of the program:

#define DEBUG

Mode four:

#ifdef identifier

block

#else

block 2

#endif

Its meaning is: if the identifier is defined, compile program segment 1, otherwise compile program segment 2.

Mode five:

#if expressions

block 1

#endif

Its meaning is: if the expression is established, then compile the program segment 1, otherwise, do not compile the program segment.

Mode six:

#if expressions

block 1

#else 

block 2

#endif 

Its meaning is: if the expression is true, compile the program segment 1, otherwise, compile the program segment 2.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325909907&siteId=291194637