Preprocessing Command Conditional Compilation

content

preprocessing command

Conditional compilation

1.#if···#endif

2.#ifdef···#endif

3.#ifndef···#endif


preprocessing command

Lines starting with # in a source program are called preprocessing directives.

Preprocessing directives are not syntactic components of the C language, but directives passed to the compiler.

include:

macro definition

#define

#undef

file contains

#include

Conditional compilation

#if

#ifdef

#else

#alif

#endif

other

#line

#error

#pragma

We wrote about macro definitions and file inclusion notes in previous articles, now let's learn about conditional compilation.

Conditional compilation

Under normal circumstances, all the statements in the source program are compiled, but sometimes it is hoped to compile different parts of the source program according to certain conditions, which is conditional compilation.

The function of conditional compilation is to make the same source program get different object code under different compilation conditions.

1.#if···#endif

#if 条件1
    程序1
#elif 条件2
      程序2
  ·
  ·
  ·
#else
      程序n
#endif

Compile program 1 if condition 1 is true, compile program 2 if condition 2 is true, and compile program n if neither is true.

illustrate:

Elif means else if, but it cannot be written as else if.

#elif and #else can be absent, but endif must be present, which is the end of the #if command.

There can be more than one #elif.

Conditions can be left without parentheses.

2.#ifdef···#endif

#ifdef 宏名
       程序1
#else
       程序2
#endif

If this macro has been defined, then compile program1, otherwise compile program2.

3.#ifndef···#endif

#ifndef 宏名
        程序1
#else
        程序2
#endif

If this macro is not defined, then compile program1, otherwise compile program2.

Guess you like

Origin blog.csdn.net/weixin_62264287/article/details/123591185