Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 1118 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#define string over multiple lines

#1
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.
Reply

#2
#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*:**

> <sub>148: Note that adjacent string literals are not concatenated into a
> single string literal </sub>

Instead use the backslash-newline :

#define EXAMPLE "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\
ccccccccccccccccccccccccccccccccccc\
ddddddddddddddddddddddddddddddddddd"


**C99 5.1.1.2 p/2**

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

Reply

#3
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](

[To see links please register here]

)

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](

[To see links please register here]

)

### Note:

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:<br>
[...]<br>
6. Adjacent string literal tokens are concatenated.<br>
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]( )
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through