Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you indent preprocessor statements?

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?

like image 665
math Avatar asked Jun 04 '10 15:06

math


People also ask

Should preprocessor directives be indented?

C static code analysis: Preprocessor directives should not be indented.

Can Ifdef be indented?

Liberal blank lines and comments are the approach I've typically taken regarding non-indented #ifdef checks. All that said, there's no rule that you can't indent preprocessor checks if it makes the code more readable.

What is preprocessor statements?

Preprocessor statements are handled by the compiler (or preprocessor) before the program is actually compiled. All # statements are processed first, and the symbols (like TRUE) which occur in the C program are replaced by their value (like 1).

Which is the preprocessor symbol?

All Preprocessor directives begin with the # (hash) symbol. C++ compilers use the same C preprocessor. The preprocessor is a part of the compiler which performs preliminary operations (conditionally compiling code, including files etc...) to your code before the compiler sees it.


1 Answers

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.

like image 66
James McNellis Avatar answered Sep 23 '22 15:09

James McNellis