Is #define banned in industry standards?

前端 未结 13 2797
长发绾君心
长发绾君心 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

    No, use of macros is not banned.

    In fact, use of #include guards in header files is one common technique that is often mandatory and encouraged by accepted coding guidelines. Some folks claim that #pragma once is an alternative to that, but the problem is that #pragma once - by definition, since pragmas are a hook provided by the standard for compiler-specific extensions - is non-standard, even if it is supported by a number of compilers.

    That said, there are a number of industry guidelines and encouraged practices that actively discourage all usage of macros other than #include guards because of the problems macros introduce (not respecting scope, etc). In C++ development, use of macros is frowned upon even more strongly than in C development.

    Discouraging use of something is not the same as banning it, since it is still possible to legitimately use it - for example, by documenting a justification.

    0 讨论(0)
  • 2020-12-23 19:31

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

    For example, section 7.5 Errors <errno.h> of the C Standard requires the use of macros:

    1 The header <errno.h> 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.

    0 讨论(0)
  • 2020-12-23 19:31

    Macros are used pretty heavily in GNU land C, and without conditional preprocessor commands there's be no way to properly handle multiple inclusions of the same source files, so that makes them seem like essential language features to me.

    Maybe your class is actually on C++, which despite many people's failure to do so, should be distinguished from C as it is a different language, and I can't speak for macros there. Or maybe the professor meant he's banning them in his class. Anyhow I'm sure the SO community would be interested in hearing which standard he's talking about, since I'm pretty sure all C standards support the use of macros.

    0 讨论(0)
  • 2020-12-23 19:34

    Contrary to all of the answers to date, the use of preprocessor directives is oftentimes banned in high-reliability computing. There are two exceptions to this, the use of which are mandated in such organizations. These are the #include directive, and the use of an include guard in a header file. These kinds of bans are more likely in C++ rather than in C.

    Here's but one example: 16.1.1 Use the preprocessor only for implementing include guards, and including header files with include guards.

    Another example, this time for C rather than C++: JPL Institutional Coding Standard for the C Programming Language . This C coding standard doesn't go quite so far as banning the use of the preprocessor completely, but it comes close. Specifically, it says

    Rule 20 (preprocessor use) Use of the C preprocessor shall be limited to file inclusion and simple macros. [Power of Ten Rule 8].


    I'm neither condoning nor decrying those standards. But to say they don't exist is ludicrous.

    0 讨论(0)
  • 2020-12-23 19:35

    Look at any header file and you will see something like this:

    #ifndef _FILE_NAME_H
    #define _FILE_NAME_H
    //Exported functions, strucs, define, ect. go here
    #endif /*_FILE_NAME_H */
    

    These define are not only allowed, but critical in nature as each time the header file is referenced in files it will be included separately. This means without the define you are redefining everything in between the guard multiple times which best case fails to compile and worse case leaves you scratching your head later why your code doesn't work the way you want it to.

    The compiler will also use define as seen here with gcc that let you test for things like the version of the compiler which is very useful. I'm currently working on a project that needs to compile with avr-gcc, but we have a testing environment that we also run our code though. To prevent the avr specific files and registers from keeping our test code from running we do something like this:

    #ifdef __AVR__
    //avr specific code here
    #endif
    

    Using this in the production code, the complementary test code can compile without using the avr-gcc and the code above is only compiled using avr-gcc.

    0 讨论(0)
  • 2020-12-23 19:38

    No your professor is wrong or you misheard something.

    #define is a preprocessor macro, and preprocessor macros are needed for conditional compilation and some conventions, which aren't simply built in the C language. For example, in a recent C standard, namely C99, support for booleans had been added. But it's not supported "native" by the language, but by preprocessor #defines. See this reference to stdbool.h

    0 讨论(0)
提交回复
热议问题