Is #define banned in industry standards?

CC PreprocessorPreprocessor Directive

C Problem Overview


I am a first year computer science student and my professor said #define is banned in the industry standards along with #if, #ifdef, #else, and a few other preprocessor directives. He used the word "banned" because of unexpected behaviour.

Is this accurate? If so why?

Are there, in fact, any standards which prohibit the use of these directives?

C Solutions


Solution 1 - C

First I've heard of it.

No; #define and so on are widely used. Sometimes too widely used, but definitely used. There are places where the C standard mandates the use of macros — you can't avoid those easily. For example, §7.5 Errors <errno.h> says:

> 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; …

Given this, it is clear that not all industry standards prohibit the use of the C preprocessor macro directives. However, there are 'best practices' or 'coding guidelines' standards from various organizations that prescribe limits on the use of the C preprocessor, though none ban its use completely — it is an innate part of C and cannot be wholly avoided. Often, these standards are for people working in safety-critical areas.

One standard you could check the MISRA C (2012) standard; that tends to proscribe things, but even that recognizes that #define et al are sometimes needed (section 8.20, rules 20.1 through 20.14 cover the C preprocessor).

The NASA GSFC (Goddard Space Flight Center) C Coding Standards simply say:

> Macros should be used only when necessary. Overuse of macros can make code harder to read and maintain because the code no longer reads or behaves like standard C.

The discussion after that introductory statement illustrates the acceptable use of function macros.

The CERT C Coding Standard has a number of guidelines about the use of the preprocessor, and implies that you should minimize the use of the preprocessor, but does not ban its use.

Stroustrup would like to make the preprocessor irrelevant in C++, but that hasn't happened yet. As Peter notes, some C++ standards, such as the JSF AV C++ Coding Standards (Joint Strike Fighter, Air Vehicle) from circa 2005, dictate minimal use of the C preprocessor. Essentially, the JSF AV C++ rules restrict it to #include and the #ifndef XYZ_H / #define XYZ_H / … / #endif dance that prevents multiple inclusions of a single header. C++ has some options that are not available in C — notably, better support for typed constants that can then be used in places where C does not allow them to be used. See also static const vs #define vs enum for a discussion of the issues there.

It is a good idea to minimize the use of the preprocessor — it is often abused at least as much as it is used (see the Boost preprocessor 'library' for illustrations of how far you can go with the C preprocessor).

Summary

The preprocessor is an integral part of C and #define and #if etc cannot be wholly avoided. The statement by the professor in the question is not generally valid: #define is banned in the industry standards along with #if, #ifdef, #else, and a few other macros is an over-statement at best, but might be supportable with explicit reference to specific industry standards (but the standards in question do not include ISO/IEC 9899:2011 — the C standard).


Note that David Hammen has provided information about one specific C coding standard — the JPL C Coding Standard — that prohibits a lot of things that many people use in C, including limiting the use of of the C preprocessor (and limiting the use of dynamic memory allocation, and prohibiting recursion — read it to see why, and decide whether those reasons are relevant to you).

Solution 2 - C

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.

Solution 3 - C

Some coding standards may discourage or even forbid the use of #define to create function-like macros that take arguments, like

#define SQR(x) ((x)*(x))

because a) such macros are not type-safe, and b) somebody will inevitably write SQR(x++), which is bad juju.

Some standards may discourage or ban the use of #ifdefs for conditional compilation. For example, the following code uses conditional compilation to properly print out a size_t value. For C99 and later, you use the %zu conversion specifier; for C89 and earlier, you use %lu and cast the value to unsigned long:

#if __STDC_VERSION__ >= 199901L
#  define SIZE_T_CAST
#  define SIZE_T_FMT "%zu"
#else
#  define SIZE_T_CAST (unsigned long)
#  define SIZE_T_FMT "%lu"
#endif
...
printf( "sizeof foo = " SIZE_T_FMT "\n", SIZE_T_CAST sizeof foo );

Some standards may mandate that instead of doing this, you implement the module twice, once for C89 and earlier, once for C99 and later:

/* C89 version */
printf( "sizeof foo = %lu\n", (unsigned long) sizeof foo );

/* C99 version */
printf( "sizeof foo = %zu\n", sizeof foo );

and then let Make (or Ant, or whatever build tool you're using) deal with compiling and linking the correct version. For this example that would be ridiculous overkill, but I've seen code that was an untraceable rat's nest of #ifdefs that should have had that conditional code factored out into separate files.

However, I am not aware of any company or industry group that has banned the use of preprocessor statements outright.

Solution 4 - C

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.

Solution 5 - C

No, #define is not banned. Misuse of #define, however, may be frowned upon.

For instance, you may use

#define DEBUG

in your code so that later on, you can designate parts of your code for conditional compilation using #ifdef DEBUG, for debug purposes only. I don't think anyone in his right mind would want to ban something like this. Macros defined using #define are also used extensively in portable programs, to enable/disable compilation of platform-specific code.

However, if you are using something like

#define PI 3.141592653589793

your teacher may rightfully point out that it is much better to declare PI as a constant with the appropriate type, e.g.,

const double PI = 3.141592653589793;

as it allows the compiler to do type checking when PI is used.

Similarly (as mentioned by John Bode above), the use of function-like macros may be disapproved of, especially in C++ where templates can be used. So instead of

#define SQ(X) ((X)*(X))

consider using

double SQ(double X) { return X * X; }

or, in C++, better yet,

template <typename T>T SQ(T X) { return X * X; }

Once again, the idea is that by using the facilities of the language instead of the preprocessor, you allow the compiler to type check and also (possibly) generate better code.

Once you have enough coding experience, you'll know exactly when it is appropriate to use #define. Until then, I think it is a good idea for your teacher to impose certain rules and coding standards, but preferably they themselves should know, and be able to explain, the reasons. A blanket ban on #define is nonsensical.

Solution 6 - C

That's completely false, macros are heavily used in C. Beginners often use them badly but that's not a reason to ban them from industry. A classic bad usage is #define succesor(n) n + 1. If you expect 2 * successor(9) to give 20, then you're wrong because that expression will be translated as 2 * 9 + 1 i.e. 19 not 20. Use parenthesis to get the expected result.

Solution 7 - C

No. It is not banned. And truth to be told, it is impossible to do non-trivial multi-platform code without it.

Solution 8 - C

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

Solution 9 - C

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.

Solution 10 - C

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.

Solution 11 - C

If you want your C code to interoperate with C++ code, you will want to declare your externally visible symbols, such as function declarations, in the extern "C" namespace. This is often done using conditional compilation:

#ifdef __cplusplus
extern "C" {
#endif

/* C header file body */

#ifdef __cplusplus
}
#endif

Solution 12 - C

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.

Solution 13 - C

If you had just mentioned #define, I would have thought maybe he was alluding to its use for enumerations, which are better off using enum to avoid stupid errors such as assigning the same numerical value twice.

Note that even for this situation, it is sometimes better to use #defines than enums, for instance if you rely on numerical values exchanged with other systems and the actual values must stay the same even if you add/delete constants (for compatibility).

However, adding that #if, #ifdef, etc. should not be used either is just weird. Of course, they should probably not be abused, but in real life there are dozens of reasons to use them.

What he may have meant could be that (where appropriate), you should not hardcode behaviour in the source (which would require re-compilation to get a different behaviour), but rather use some form of run-time configuration instead.

That's the only interpretation I could think of that would make sense.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpsrag anveshView Question on Stackoverflow
Solution 1 - CJonathan LefflerView Answer on Stackoverflow
Solution 2 - CPeterView Answer on Stackoverflow
Solution 3 - CJohn BodeView Answer on Stackoverflow
Solution 4 - CAndrew HenleView Answer on Stackoverflow
Solution 5 - CViktor TothView Answer on Stackoverflow
Solution 6 - Cmikedu95View Answer on Stackoverflow
Solution 7 - Cluis.espinalView Answer on Stackoverflow
Solution 8 - CSuperlokkusView Answer on Stackoverflow
Solution 9 - CDaniel FarrellView Answer on Stackoverflow
Solution 10 - CDavid HammenView Answer on Stackoverflow
Solution 11 - CjxhView Answer on Stackoverflow
Solution 12 - CDomView Answer on Stackoverflow
Solution 13 - CjcaronView Answer on Stackoverflow