How do you indent preprocessor statements?
Asked Answered
D

5

31

When there are many preprocessor statements and many #ifdef cascades, it's hard to get an overview since normally they are not indented. e.g.

#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#ifdef SOMEOTHER
stmts
#endif
maybe stmts
#endif

When I consider also indenting those preprocessor statements, I fear of getting confused with the general indentation level. So how do you solve this in a beautiful way?

Doubleheader answered 4/6, 2010 at 15:2 Comment(1)
I spend more time removing #ifdef's than I do indenting them. :-)Dam
G
29

Just because preprocessing directives are "normally" not indented is not a good reason not to indent them:

#ifdef __WIN32__
    #include <pansen_win32>
#else
    #include <..>
    #ifdef SOMEOTHER
        stmts
    #endif
    maybe stmts
#endif

If you frequently have multiple levels of nesting of preprocessing directives, you should rework them to make them simpler.

Gogetter answered 4/6, 2010 at 15:9 Comment(2)
Pure ANSI C doesn't allow this: See https://mcmap.net/q/24870/-indenting-defines.Noeminoesis
@DipanMehta: That is not what that thread says. That thread says that pre-standard C did not allow indentation of preprocessing directives. That may be true (I have no idea, and it doesn't much matter since C has been standardized for 22 years now). However, in Standard C (C90 and C99, both often called "ANSI C"), and in C++, a preprocessing directive may be indented.Gogetter
G
28

As you, I didn't make my mind yet about the best way to indent, but I found in more than one place this alternative indentation in which the # is placed always at the first column and just the keyword is indented:

#ifdef __WIN32__
#  include <pansen_win32>
#else
#  include <..>
#endif

In Visual Studio, when you type # as the first character it always brings its indentation to the left, so it seems that MS either favors never indenting preprocessor statements, or using the above format.

The big problem is when you have non-preprocessor and preprocessor statements mixed and indentation applied. It's hard to make code that looks good, no matter which option:

option (a)

for (...)
{
  if (foo)
  {
    if (bar)
    {
#ifdef __WIN32__
      c = GetTickCount();
#else
      c = clock();
#endif
    }
  }
}

option (b)

for (...)
{
  if (foo)
  {
    if (bar)
    {
      #ifdef __WIN32__
      c = GetTickCount();
      #else
      c = clock();
      #endif
    }
  }
}

option (c)

for (...)
{
  if (foo)
  {
    if (bar)
    {
#     ifdef __WIN32__
        c = GetTickCount();
#     else
        c = clock();
#     endif
    }
  }
}

At this point, it becomes a question of personal taste, as so many other indentation styles.

Guayaquil answered 4/6, 2010 at 15:43 Comment(1)
I vote for Option b, but with the c = ... statements indented, tooKreda
K
7

This question already has lots of valid answers. For people that want a visual image here it is.

In visual studio go to options search for indent and select your language. In my case it is c++. As you toggle between the options visual studio will show you an example bellow.

enter image description here

Knavery answered 20/7, 2020 at 13:37 Comment(0)
S
0

First, make sure you actually need all the ifdef statements. Perhaps there's a way to refactor them to keep the number of nested if checks limited? Assuming you do need them:

It would be a good idea to separate the first portion (the includes) into a separate include file:

In your_header.h:

#ifdef __WIN32__
#include <pansen_win32>
#else
#include <..>
#endif

Then in the implementation file, you can then keep things more sane. Liberal blank lines and comments are the approach I've typically taken regarding non-indented #ifdef checks.

#ifndef __WIN32__
#ifdef SOMEOTHER

stmts

// SOMEOTHER
#endif

maybe stmts

// __WIN32__
#endif

All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable.

Solicitude answered 4/6, 2010 at 15:15 Comment(0)
P
-1

"All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable."

No, but in a world where style is controlled and the code is adjusted to match it would be nice to enable the style to indent #if statements like if statements.

While #if might technically be a different language it is part of the C/C++ specification and as it is not a letter for letter match of any current keywords there is no reason that #ifs can not be accommodated for in the styling.

Postmillennialism answered 19/1, 2016 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.