Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is #define banned in industry standards?

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?

like image 746
psrag anvesh Avatar asked Dec 28 '15 15:12

psrag anvesh


People also ask

What are the meanings of is?

Definition of is (Entry 1 of 4) present tense third-person singular of be. dialectal present tense first-person and third-person singular of be. dialectal present tense plural of be.

Is a is a verb?

Is is a verb or a noun? Is it a preposition? In this post, we have learned that the word is a verb and functions solely as a verb to describe a state of being or existence. Is is a verb.

Is in use a word?

Definition of in use : being used All of the computers are currently in use.

How do you write as is?

As-is definition Alternative spelling of as is. (idiomatic, of an object) As it is; its present state or condition, especially as a contractual condition of sale. I bought the car as is, so the seller was within his legal rights to refuse to repair it when it broke down after two days.


2 Answers

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).

like image 102
Jonathan Leffler Avatar answered Oct 19 '22 12:10

Jonathan Leffler


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.

like image 40
Peter Avatar answered Oct 19 '22 11:10

Peter