I was wondering how I could define a really long string over the multiple lines. I tried so many different patterns, but none of them is working.. Here is my code.
#define EXAMPLE "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"ccccccccccccccccccccccccccccccccccc"
"ddddddddddddddddddddddddddddddddddd"
and I get syntax error. The error I got is
ccompile.h (as included in test.c)
=================
error: syntax error before or at: g
*** Error code 2
I want to assign "aaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbccccccccccccccdddddddd" to EXAMPLE.
I tried using \ and @\ but that didn't work out.
Just get rid of the whitespace in between the lines, and quote the whole thing. A \
at the EOL basically "escapes" the newline, so it won't be part of the string itself. It's only relevant for the preprocessor:
#include <stdio.h>
#define LONG_STRING "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
int main ( void ) {
printf(LONG_STRING);
return 0;
}
That works just fine
For aesthetic reasons, you can quote each line separately, the only requirement is you add the \
directly after the closing quotes:
#define LONG_STRING "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"\
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"\
"ccccccccccccccccccccccccccccccccccccccccccccc"
This, too, works just fine
The two suggestions are not 100% equivalent. The first version defines a macro to be a single string literal. The second version defines the macro as 3 separate string literals. For the most part, this isn't a big deal, because during the translation phase, adjacent string literal tokens should be concatenated:
5.1.1.2 Translation phases:
[...]
6. Adjacent string literal tokens are concatenated.
7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.
I could not find the footnote Meninx mentions about C99 behaving differently. Document I used can be found here
#define EXAMPLE "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
"ccccccccccccccccccccccccccccccccccc"
"ddddddddddddddddddddddddddddddddddd"
For the directive above, its replacement list is limited only to "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
because macro definition works only a single logical line.
Moreover, concatenating adjacent string literals to form a single one is not possible in C during preprocessing.
C99 footnote 148:
148: Note that adjacent string literals are not concatenated into a single string literal
Instead use the backslash-newline :
#define EXAMPLE "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\
ccccccccccccccccccccccccccccccccccc\
ddddddddddddddddddddddddddddddddddd"
C99 5.1.1.2 p/2
- Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With