Advantages of conditional-preprocessor over conditional statements

后端 未结 5 2054
遇见更好的自我
遇见更好的自我 2021-01-12 07:30

I have never worked with #if, #ifdef, #ifndef, #else, #elif and #endif

5条回答
  •  攒了一身酷
    2021-01-12 07:48

    The example you showed doesn't seem helpful due to lack of other information. But here's an example that #if is useful.

    #if OS == LINUX
    //do something
    #elif OS == SOLARIS
    //do something else
    #else
    //
    #endif
    

    The key is that #if is evaluated in compile time, but if is evaluated when program runs.

    #if BYTE_ORDER == LITTLE_ENDIAN
    //do something
    #else
    //do something else
    #endif
    

提交回复
热议问题