简体   繁体   中英

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?

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

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.

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;

这意味着define语句超过一行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM