GCC: Preprocessing statements--#if, #elif, #else, #endif和 #ifdef, #ifndef

Source: Internet
Author: User
Tags arithmetic arithmetic operators

Original articles, reproduced please indicate the source, thank you.
Author: Qing Lin, blog name: Flying Air and static crossing

#if语句

#if语句会计算它后面的表达式, and checks the result of the expression, if the result is true (true), the subsequent statement is compiled, and if False (false), the subsequent statement is not compiled.

For example:

#if COUNT
Char *desc = "The Count is Non-zero";
#endif


Compile only if Count is not 0

Char *desc = "The Count is Non-zero";

Statement.

Here are some of the syntax rules for an expression that follows # if:

1) expressions can include shaping constants and macros, which must be assigned (the difference is defined)

2) You can use parentheses to specify the order of the expressions.

3) expressions can contain +,-,*,/,<< and >> arithmetic operators in the C language. These arithmetic operations use the value of the maximum representation of the integer type of the current platform, typically a 64-bit integer value.

4) Expressions can also include <,>,<=,>= and = = comparison operators.

5) expressions can include && and | | The logical operator.

6) The non-(!) operator reverses the result of an expression, such as: #if! (Limxp > 12)

If LIMXP > 12, then the result of the expression is false

7) #if也可以和defined语句一起使用 so that you can check whether a macro has been defined (or you can use #ifdef directly). Such as:

#if defined (MINXP)

This means that if MINXP is defined, then the expression is true. You can also precede the result with a non-(!) operator, such as:

#if!defined (MINXP)

This means that if MINXP is not defined, then the expression is true.

8) If an identifier is not defined, its value is always the 0,-WUNDEF option that can be used to generate this warning message.

9) Macros with parameters (GCC: Preprocessing statements--#define, #error和 #warning) are also 0 values in the expression, and the-WUNDEF option can be used to generate this warning message.

) #else提供另外的选择, such as:

#if mintxt <= 5
#define Mintlog 11
#else
#define Mintlog 14
#endif
One) #elif可以提供一个或多个的选择表达式, such as:

#if mintxt <= 5
#define Mintlog 11
#elif Mintxt = = 6
#define Mintlog 12
#elif Mintxt = = 7
#define Mintlog 13
#else
#define Mintlog 14
#endif

#ifdef

If the macro that follows the same line as the #ifdef is defined, then the statement block after the #ifdef is compiled, and the block ends with #endif. For example:

#ifdef Mintarray
int xarray[20];
#endif/* Mintarray */

#endif后面的注释语句并不是必须的/* Mintarray * *, but it's a good way to do it, so you know which macro you defined is the end position.

and #ifdef the idea of the opposite is #ifndef.

Please refer to the GCC manual for more information.

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.