[comp.lang.c] structured entry of #if preprocessor commands

umsmigie@ccu.umanitoba.ca (Jason Smigiel) (02/10/91)

	 
 
Is it common practice not to structure preprocessor commands in ANSI C?
 
I've been staring at code that reads like this:
 
   #if defined(MSDOS)
   #if defined(ANSI)
   #include "ms_ansi.h"
   #endif
   #else /* not msdos */
   #if !defined(ATARIST_MWC) && !defined(MAC) && !defined(AMIGA)
   #ifndef VMS
   #include <sys/ioctl.h>
   #endif
   #include <signal.h>
   #endif
   #endif
 
While it makes a lot more sense to me to write it something like this:
 
   #if defined(MSDOS)
      #if defined(ANSI)
      #include "ms_ansi.h"
      #endif
   #else /* not msdos */
      #if !defined(ATARIST_MWC) && !defined(MAC) && !defined(AMIGA)
         #ifndef VMS
         #include <sys/ioctl.h>
         #endif
      #include <signal.h>
      #endif
   #endif
 
I looked in the K&R2 reference manual, under A12,(very first paragraph) and
it states:
 
    "Lines beginning with #, perhaps preceeded by white space, communicate
    with this preprocessor."
 
  So it seems completely valid to me, I just wanted to know if this was a smart
thing to do or am I entirely insane?
 
Jason Smigiel
umsmigie@ccu.umanitoba.ca

scs@adam.mit.edu (Steve Summit) (02/11/91)

In article <1991Feb10.063915.5476@ccu.umanitoba.ca>, Jason Smigiel writes:
>Is it common practice not to structure preprocessor commands in ANSI C?
>I looked in the K&R2 reference manual, under A12,(very first paragraph) and
>it states:
> 
>    "Lines beginning with #, perhaps preceded by white space, communicate
>    with this preprocessor."
> 
>  So it seems completely valid to me, I just wanted to know if this was a
>smart thing to do or am I entirely insane?

You're not insane.  However, leading whitespace before
preprocessor directives is a relatively new "innovation."
Older compilers do not allow it, and there are enough of them
around that people tend not to use leading whitespace at all.
Furthermore, those of us who have been using the older compilers
for years are so used to it that nicely indented nested #if's
look, well, pretty weird.  You're correct, though, in thinking
that more nicely structured #if's would be better.

What I can't explain is why the following style, which most old
compilers would accept, has never been widespread (and which
therefore looks equally weird to me):

   #if defined(MSDOS)
   #   if defined(ANSI)
   #   include "ms_ansi.h"
   #   endif
   #else /* not msdos */
   #   if !defined(ATARIST_MWC) && !defined(MAC) && !defined(AMIGA)
   #      ifndef VMS
   #      include <sys/ioctl.h>
   #      endif
   #   include <signal.h>
   #   endif
   #endif

For all the ranting that I and others do about the importance of
good style and clear program layout, there are always these
little hypocrisies.

                                            Steve Summit
                                            scs@adam.mit.edu

datangua@watmath.waterloo.edu (David Tanguay) (02/11/91)

In article <1991Feb10.204604.28908@athena.mit.edu> scs@adam.mit.edu writes:
|For all the ranting that I and others do about the importance of
|good style and clear program layout, there are always these
|little hypocrisies.
|                                            Steve Summit

Corollory: All the logical, rational, and pragmatic arguments in the world
will not convince my *eyes* that your code layout is better than mine. :-)
-- 
David Tanguay            Software Development Group, University of Waterloo

barmar@think.com (Barry Margolin) (02/11/91)

In article <1991Feb10.204604.28908@athena.mit.edu> scs@adam.mit.edu writes:
>What I can't explain is why the following style, which most old
>compilers would accept, has never been widespread
>   #if defined(MSDOS)
>   #   if defined(ANSI)

I think it's because most people think of the "#" and the keyword as one
token ("#if"), even though the language officially considers them
independent (the "#" preprocessor indicator followed by the "if" pp
command).

--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

gwyn@smoke.brl.mil (Doug Gwyn) (02/11/91)

In article <1991Feb10.063915.5476@ccu.umanitoba.ca> umsmigie@ccu.umanitoba.ca (Jason Smigiel) writes:
>Is it common practice not to structure preprocessor commands in ANSI C?

You mean, not to indent them.  The answer is, yes.

>So it seems completely valid to me, I just wanted to know if this was a smart
>thing to do or am I entirely insane?

Many existing (pre-ANSI) C preprocessors are not tolerant of white space
before the #.

dgil@pa.reuter.COM (Dave Gillett) (02/12/91)

In <1991Feb10.063915.5476@ccu.umanitoba.ca> umsmigie@ccu.umanitoba.ca (Jason Smigiel) writes:

>Is it common practice not to structure preprocessor commands in ANSI C?
                              ^^^^^^^^^

     The word "structure", as a noun and a verb, has a variety of meanings
that are useful in a programming context.

     However, its use as a verb synonymous (according to Jason's example) with
"indent" is not among those.  Indentation is commonly used as a technique to
make structure visible, but it is not structure in a programming sense and
should never be mistaken for it.

                                                Dave

daw@cbnewsh.att.com (David Wolverton) (02/14/91)

In article <1991Feb10.204604.28908@athena.mit.edu>, scs@adam.mit.edu (Steve Summit) writes:
> What I can't explain is why the following style, which most old
> compilers would accept, has never been widespread (and which
> therefore looks equally weird to me):
> 
>    #if defined(MSDOS)
>    #   if defined(ANSI)
>    #   include "ms_ansi.h"
>    #   endif
>    #else /* not msdos */
>    #   if !defined(ATARIST_MWC) && !defined(MAC) && !defined(AMIGA)
>    #      ifndef VMS
>    #      include <sys/ioctl.h>
>    #      endif
>    #   include <signal.h>
>    #   endif
>    #endif

I experimented with this style, but never really fully adopted
it, partly because it isn't widespread, as Steve points out.

But another reason was the early lack of "#elif", which makes
the indentation sometimes look a little weird:

#if CHOICE_1
#   define FOO "abcd"
#else				/* would rather have a single */
#if CHOICE_2			/* #elif CHOICE_2 here */
#   define FOO "efgh"
#else
#if CHOICE_3
#   define FOO "ijkl"
#endif				/* trebled #endif's due to lack */
#endif				/* of #elif */
#endif

Unfortunately, it often seemed to me that precisely the
times when using indentation would clarify the meaning
were also the times when #elif was useful.

Dave Wolverton
daw@attunix.att.com