masmic
masmic

Reputation: 3564

Why do multi-line macros have backslashes at the end of each line?

In a macro declaration like:

#define WAIT_SPI2_TRANSMISSON_END() {while ((SPI2_SR & SPI_SR_TXCTR_MASK) != 0) {\
                                     if( SPI2_SR & SPI_SR_RFDF_MASK ) {\
                                       (void)SPI2_POPR;\
                                       SPI2_SR |= SPI_SR_RFDF_MASK ;\
                                     }}\

What do these backslashes (\) mean or do there?

Upvotes: 21

Views: 10803

Answers (4)

Artur
Artur

Reputation: 7257

It is so called continued line. It means that your line is continued in line that follows. It is simply sometimes easier to read stuff if written this way.

BTW - continued lines are 'glued' at preprocessor pass.

Read here about step #3: gcc docs

Excerpt:

 /\
 *
 */ # /*
 */ defi\
 ne FO\
 O 10\
 20

is equivalent to:

#define FOO 1020

It is noteworthy to say that continued lines do not have to be used within preprocessor macros. It is perfectly legal top write this:

f\
lo\
at f = 5.0; 

which is the same as:

float f = 5.0;

Upvotes: 9

Arne Mertz
Arne Mertz

Reputation: 24606

The slashes are used to make the following end of line a non-linebreak for the preprocessor. A #define has to be exactly one line for the preprocessor. To augment readability you can use the backslashes before the end of lines. The preprocessor will first erase any linebreaks preceded by a backslash and only after that parse the #define. So while you see multiple lines, the PP sees only one.

Upvotes: 15

Bathsheba
Bathsheba

Reputation: 234715

It's a line continuation character.

There should be nothing else after it (aside from an end of line character), including white space.

It's particularly useful for macros as it adds clarity.

(Very, very occasionally - especially in old code - you'll see the trigraph sequence ??/ in place of \. These days though it's more of an interviewers' trick question.)

Upvotes: 18

Blacktempel
Blacktempel

Reputation: 3995

This means that the define statement goes over more than one line.

Upvotes: 4

Related Questions