Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C macro/#define indentation?

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?

like image 917
Michael Avatar asked Dec 06 '09 06:12

Michael


People also ask

Can we print macro in C?

Here we will see how to define a macro called PRINT(x), and this will print whatever the value of x, passed as an argument. To solve this problem, we will use the stringize operator. Using this operator the x is converted into string, then by calling the printf() function internally, the value of x will be printed.

What type are macros in C?

In C, A macro is any constant value or variable with its value. And the macro name will get replaced by its value in the entire program. Macros help in writing less code and also in saving time.

What is object macro in C?

An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants. You create macros with the ' #define ' directive. ' #


4 Answers

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 }.

like image 143
brainiac Avatar answered Oct 28 '22 08:10

brainiac


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 '#'.

like image 35
Seth Avatar answered Oct 28 '22 06:10

Seth


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.

like image 36
Steve Jessop Avatar answered Oct 28 '22 07:10

Steve Jessop


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.

like image 45
whacko__Cracko Avatar answered Oct 28 '22 08:10

whacko__Cracko