23

A while ago I have discovered an (rather ancient) C Compiler, which scanned macros this way (Pseudo code):

 if line.startswith("#include") or line.startswith("#define"):
     ...

.. Which kind of raised the question for me where macros should really be placed, at the beginning of a line, like so:

void stuff()
{
#if defined(WIN32) || defined(_WIN32)
    ...
#else
#if defined(__GNUC__)
    ...
#else
    ...
#endif
#endif
}

Or rather like so (as that's the way I do it, for improved readability):

void stuff()
{
    #if defined(WIN32) || defined(_WIN32)
    ...
    #else
    #   if defined(__GNUC__)
    ...
    #   else
    ...
    #   endif
    #endif
}

Is the way one indents the Preprocessor code standardized, that is, no matter how i indent it, it will always work the same way?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

5 Answers5

20

Some old C compilers required that the #define (for example) be flush with the left margin:

#define FOO bar

Other C compilers required only that the # be at the left margin, so you could:

#    define FOO bar

Newer C compilers tend to accept the # after any leading whitespace:

    #define FOO bar

If you want compatibility with such older compilers, you should at least put your # in the first column. If compatibility doesn't matter, then it's up to you.

I would usually try not to embed #ifdef blocks inside functions, so the whole question of whether they should be indented mostly goes away.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • I guess I wouldn't have to worry about support for compilers like ZetaC, which are fortunately only very rarely used anymore ;-) –  Jan 18 '11 at 09:09
  • "If you want compatibility with such older compilers" - then you'll need to work out where else they don't even implement C89, let alone C99? – Steve Jessop Jan 18 '11 at 10:04
  • You're talking pre-ANSI K&R v1 compilers there. So forget about function declarations and parameter lists. – MSalters Jan 18 '11 at 13:41
15

from gcc C preprocessor documentation:

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

davka
  • 13,974
  • 11
  • 61
  • 86
  • @Will: you probably wrote this comment before I added the second sentence to the quotation :) – davka Jan 18 '11 at 08:51
2

No, they don't need to be at the beginning of the line, but they can only have blanks (spaces, tabs, ...) before them.

Usually they're put at the beginning of the line because they're not subjected to the scopes they're into, since they're preprocessed before actual C code.

peoro
  • 25,562
  • 20
  • 98
  • 150
  • Whitespace is allowed after the `#`, like in ` # include ` – David Rodríguez - dribeas Jan 18 '11 at 08:54
  • @peoro I guess that "they can only have blanks before them" is ambiguous - i.e. it could mean "they can have blanks before them, but not anywhere else" or it could mean "before them they can only have blanks, and not anything else". Of course you meant the latter, but the wording is ambiguous. – davmac Nov 06 '18 at 12:26
-1

Doesn't matter. See it this way, if code was not idented and in 1 line it still should compile(only code, preprocessor/includes at some other things needs a seperate line).

Edit: seems to be that really old compiler are picky about this. Preprocessor should be at one line, just like other non-code things like includes

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • 1
    If the code was not indented an all on one line, it would not compile: the preprocessor is line-sensitive. – Fred Nurk Jan 18 '11 at 09:00
  • yeah forgot that part. Preprocessor needs to be on 1 line(without anything else), but for code this doesn't matter. – RvdK Jan 20 '11 at 09:59
-3

I don't think the compiler "cares" if you have spaces before the preprocessed - it should be the same...

SivGo
  • 226
  • 1
  • 2