Is #define banned in industry standards?

前端 未结 13 2795
长发绾君心
长发绾君心 2020-12-23 18:52

I am a first year computer science student and my professor said #define is banned in the industry standards along with #if, #ifdef, <

13条回答
  •  無奈伤痛
    2020-12-23 19:31

    Macros can not be "banned". The statement is nonsense. Literally.

    For example, section 7.5 Errors of the C Standard requires the use of macros:

    1 The header defines several macros, all relating to the reporting of error conditions.

    2 The macros are

    EDOM
    EILSEQ
    ERANGE
    

    which expand to integer constant expressions with type int, distinct positive values, and which are suitable for use in #if preprocessing directives; and

    errno
    

    which expands to a modifiable lvalue that has type int and thread local storage duration, the value of which is set to a positive error number by several library functions. If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name errno, the behavior is undefined.

    So, not only are macros a required part of C, in some cases not using them results in undefined behavior.

提交回复
热议问题