zoukankan      html  css  js  c++  java
  • #if, #elif, #else, #endif学习

    These preprocessing directives create conditional compiling parameters that control thecompiling of the source code. They must begin on a separate line.

    Syntax:

    #if constant_expression
    #else
    #endif

    or

    #if constant_expression
    #elif constant_expression
    #endif

    The compiler only compiles the code after the #if expression if the constant_expressionevaluates to a non-zero value (true). If the value is 0 (false), then the compiler skips the linesuntil the next #else, #elif, or #endif. If there is a matching #else, and the constant_expressionevaluated to 0 (false), then the lines between the #else and the #endif are compiled. If there is amatching #elif, and the preceding #if evaluated to false, then the constant_expression after thatis evaluated and the code between the #elif and the #endif is compiled only if this expressionevaluates to a non-zero value (true).

    Examples:

    int main(void)
    {
    #if 1
    printf("Yabba Dabba Do!\n");
    #else
    printf("Zip-Bang!\n");
    #endif
    return 0;
    }

    Only "Yabba Dabba Do!" is printed.

    int main(void)
    {
    #if 1
    printf("Checkpoint1\n");
    #elif 1
    printf("Checkpoint2\n");
    #endif
    return 0;
    }

    Only "Checkpoint1" is printed. Note that if the first line is #if 0, then only "Checkpoint2" wouldbe printed.

    #if OS==1
    printf("Version 1.0");
    #elif OS==2
    printf("Version 2.0");
    #else
    printf("Version unknown");
    #endif

    Prints according to the setting of OS which is defined with a #define.

    1.7.2 #define, #undef, #ifdef, #ifndef

    The preprocessing directives #define and #undef allow the definition of identifiers whichhold a certain value. These identifiers can simply be constants or a macro function. Thedirectives #ifdef and #ifndef allow conditional compiling of certain lines of code based onwhether or not an identifier has been defined.

    Syntax:

    #define identifier replacement-code

    #undef identifier

    #ifdef identifier
    #else or #elif
    #endif

    #ifndef identifier
    #else or #elif
    #endif

    #ifdef identifier is the same is #if defined( identifier).
    #ifndef identifier is the same as #if!defined(identifier).
    An identifier defined with #define is available anywhere in the source code until a#undef is reached.
    A function macro can be defined with #define in the following manner:

    #define identifier(parameter-list) (replacement-text)

    The values in the parameter-list are replaced in the replacement-text.

    Examples:

     
    #define PI 3.141
    printf("%f",PI);

    #define DEBUG
    #ifdef DEBUG
    printf("This is a debug message.");
    #endif

    #define QUICK(x) printf("%s\n",x);
    QUICK("Hi!")

    #define ADD(x, y) x + y
    z=3 * ADD(5,6)

     

    This evaluates to 21 due to the fact that multiplication takes precedence over addition.

    #define ADD(x,y) (x + y)
    z=3 * ADD(5,6)

    This evaluates to 33 due to the fact that the summation is encapsulated in parenthesis whichtakes precedence over multiplication.

    1.7.3 #include

    The #include directive allows external header files to be processed by the compiler.

    Syntax:

    #include <header-file>

    or

    #include "source-file"

    When enclosing the file with < and >, then the implementation searches the knownheader directories for the file (which is implementation-defined) and processes it. Whenenclosed with double quotation marks, then the entire contents of the source-file is replaced atthis point. The searching manner for the file is implementation-specific.

    Examples:

    #include <stdio.h>
    #include "my_header.h"

    1.7.4 #line

    The #line directive allows the current line number and the apparent name of the currentsourcecode filename to be changed.

    Syntax:

    #line line-number filename

    Note that if the filename is not given, then it stays the same. The line number on thecurrent line is one greater than the number of new-line characters (so the first line number is 1).
    Examples:

    #line 50 user.c

    #line 23

    1.7.5 #error

    The #error directive will cause the compiler to halt compiling and return with thespecified error message.

    Syntax:

    #error message

    Examples:

    #ifndef VERSION
    #error Version number not specified.
    #endif

    1.7.6 #pragma

    This #pragma directive allows a directive to be defined. Its effects areimplementation-defined. If the pragma is not supported, then it is ignored.

    Syntax:

    #pragma directive

    1.7.7 Predefined Macros

    The following macros are already defined by the compiler and cannot be changed.

    __LINE__ A decimal constant representing the current line number.
    __FILE__ A string representing the current name of the source code file.
    __DATE__ A string representing the current date when compiling began for the currentsource file. It is in the format "mmm dd yyyy", the same as what is generated bythe asctime function.
    __TIME__ A string literal representing the current time when cimpiling began for the currentsource file. It is in the format "hh:mm:ss", the same as what is generated by theasctime function.
    __STDC__ The decimal constant 1. Used to indicate if this is a standard C compiler.
  • 相关阅读:
    Sublime Text 3
    JobTracker等相关功能模块初始化
    .NET编程规范
    理解多线程设计模式(转)
    理解java中的ThreadLocal 专题
    情商--人生职场
    老师只喜欢好学生(转)
    不是因为项目让你不能发光,而是因为你才让项目不能发光
    考试系统--前进/后退功能
    tomcat配置文件server.xml具体解释
  • 原文地址:https://www.cnblogs.com/xiangshancuizhu/p/1924011.html
Copyright © 2011-2022 走看看