views:

318

answers:

4

I'm curious as to why I see nearly all C macros formatted like this:

#ifndef FOO
#   define FOO
#endif

Or this:

#ifndef FOO
#define FOO
#endif

But never this:

#ifndef FOO
    #define FOO
#endif

(moreover, vim's = operator only seems to count the first two as correct.)

Is this due to portability issues among compilers, or is it just a standard practice?

+2  A: 

IIRC, older C preprocessors required the # to be the first character on the line (though I've never actually encountered one that had this requirement).

I never seen your code like your first example. I usually wrote preprocessor directives as in your second example. I found that it visually interfered with the indentation of the actual code less (not that I write in C anymore).

The GNU C Preprocessor manual says:

Preprocessing directives are lines in your program that start with '#'. Whitespace is allowed before and after the '#'.

Seth
And there's the additional irritation (like the OP mentioned in the question) that editors often have their own idea of how preprocessor conditional blocks should be (or not be) indented.
Michael Burr
The old VAX C compiler (circa 1986) would throw an error if a preprocessor directive didn't have the `#` in the first column.
John Bode
+3  A: 

I've seen it done all three ways, it seems to be a matter of style, not of syntax

While usually the second example is the most common, i've seen cases where the first (or third) is used to help distinguish multiple levels of #ifdefs. Sometimes the logic can become deeply nested and the only way to understand it at a glance is to use indentation much like it is common practice to indent blocks of code between { and }.

brainiac
+2  A: 

Preprocessor directives are lines included in our programs that are not actually program statements but directives for the preprocessor. These lines are always preceded by a hash sign (#).Whitespace is allowed before and after the '#'. As soon as a newline character is found, the preprocessor directive is considered to end.

There is no other rule as far the standard of C/C++ concerned,So it remains as the matter of style and readability issue,I have seen/wrote programs only in the second way that you posted,although the third one seems more readable.

nthrgeek
+1  A: 

For preference I use the third style, with the exception of include guards, for which I use the second style.

I don't like the first style at all - I think of #define as being a preprocessor instruction, even though really of course it isn't, it's a # followed by the preprocessor instruction define. But since I do think of it that way, it seems wrong to separate them. I expect text editors written by people who advocate that style will have a block indent/un-indent that works on code written in that style. But I would hate to encounter it using a text editor that didn't.

There's no point pandering to ancient preprocessors where the # has to be the first character of the line, unless you can also list off the top of your head all the other differences between those implementations and standard C, in order to avoid the other things you could possibly do that they would not support. Of course if you genuinely are working with a pre-standard compiler, fair enough.

Steve Jessop