Splitting up a char* in C

TheSpook

Limp Gawd
Joined
Feb 12, 2004
Messages
379
char* question = "Let's say I
wanted to declare a single string
on multiple lines";​

Is there an escape character that will allow gcc to recognize that I'm doing this? I ask because I'm using some really long string literals (SQL queries) and am sick of having them run off the screen . . .


Thanks :).
 
C++ concatenatenates adjacent string literals.

Code:
static const char* pstr = "So just doing this "
   "will put the strings "
   "all together when the compiler "
   "emits the initializer for the constant.";
 
Use the '\' character.

Like so:

Code:
char *str = "Hello \
there."

edit: I think that's the way, I haven't used it for a while.
 
BillLeeLee said:
Use the '\' character.
This'll work, but it's a hard technique in practice. Anything that's accidentally added after the backwhack ends up making the backwhack not work, so you end up with odd error messages and no clear reason for them (unless you have an editor that makes whitespace visible, and use that feature).

Then, everything from the beginning of the next line makes it into the string, and you have no choice about it. Your string is really "Hello there.", with 20-something spaces between the words.
 
Backwhack (excellent name, if I do say so myself) method it is!

Adding in twenty-something spaces or tabs won't really matter in this case -- SQL's mighty nice about parsing through whitespace, unlike this pesky C.

Thanks.


Edit: I do agree with your sentiments, though . . . I feel like this is a cheap hack of sorts, but . . . well, it's all the same in the end :(.
 
Prefer to use auto-concatenation of adjacent literals.

If you don't believe me, try (C++, though the C would be similar)

Code:
#include <iostream>

char const * const display_str = "Hello\
 World";

int main()
{
    std::cout << display_str << std::endl;
}

versus:

Code:
#include <iostream>

char const * const display_str = "Hello\ 
 Compiler Error";

int main()
{
    std::cout << display_str << std::endl;
}

and contrast both with:

Code:
#include <iostream>

char const * const display_str = 
"Hello" 
" World";

int main()
{
    std::cout << display_str << std::endl;
}

When you spot the error in the second program, you will understand the problem of the backwhack.
 
I didn't know C allowed adjacent string literals (only ever knew the \ method). Is this in ANSI C99 standard, or have I just been living under a rock?

I mainly code in C++ rather than C so pardon the ignorance.
 
TheSpook said:
Backwhack (excellent name, if I do say so myself) method it is!
Did you miss my post about why this isn't a desireable method? Most folks use implicit concatenation since it's simpler and less dangerous.

TheSpook said:
SQL's mighty nice about parsing through whitespace, unlike this pesky C.
Huh? C and C++ ignores whitespace outside of literals, just like SQL.
 
BillLeeLee said:
I didn't know C allowed adjacent string literals (only ever knew the \ method). Is this in ANSI C99 standard, or have I just been living under a rock?
It's in ISO C99, yes. See 5.1.1.2.6. I don't think it is in K&R C, but I don't remember when it was introduced.

It's in C++, too. See 2.1.6 of ISO/IEC:14882, or section 5.2.2 of Stroustrup 3rd.

BillLeeLee said:
I mainly code in C++ rather than C so pardon the ignorance.
Of course! Nobody came out of the womb knowing this stuff.
 
mikeblas said:
Did you miss my post about why this isn't a desireable method? Most folks use implicit concatenation since it's simpler and less dangerous.

At the time of my post, the only solution given was the backwhack method -- your post just said it was a bad way to do things, but didn't offer an alternative route. Bad way > no way, at least in this case.


Adjacent string literals are now being used, so don't get too upset ;). Thanks again.
 
I haven't read every post, but i didn't notice anything about the heap memory allocation. Don't forget to malloc/calloc your dynamic memory if your using pointers.

backwhack .... i love that name. In fact it should be a standard because half of th TV commercials get it wrong.
 
TheSpook said:
At the time of my post, the only solution given was the backwhack method -- your post just said it was a bad way to do things, but didn't offer an alternative route.
That's odd. When I view the thread, my Post #3 (suggesting adjacent concatenation) is ahead of BillLeeLee's Post #4 (suggesting backwhack). It's only by a minute, but Post #3 is certainly ahead of your Post #6.

TheSpook said:
Adjacent string literals are now being used, so don't get too upset ;). Thanks again.
I'm not upset at all -- I just wanted to know if you found some viable reason to use backwhack. And if you did, I wanted to learn about it.

MadJuggla9 said:
Don't forget to malloc/calloc your dynamic memory if your using pointers.
Pointers can be used without allocating dynamic memory. Dynamic memory isn't involved in this thread.
 
mikeblas said:
It's in ISO C99, yes. See 5.1.1.2.6. I don't think it is in K&R C, but I don't remember when it was introduced.

Quote from K&R C, chapter 2 page 38:

The Man said:
A string constant, or string literal, is a sequence of zero or more characters surrounded by double quotes, as in

"I am a string"

or
"" /* the empty string */

The quotes are not part of the string, but serve only to delimit it. The same escape sequence used in character constants apply in strings; \" represents the doub-equote character. String constants can be concatentated at compile time:

"hello," " world"

is equivelent to

"hello, world"

This is useful for splitting long strings across several source lines.

It doesn't mention anything about using \ to split lines (so don't do it! :p)
 
doh said:
Quote from K&R C, chapter 2 page 38:
Hey, there you go. Since the dawn of time, and so it was, and all that.
 
If anybody is curious about the actual purpose of the \ (backwhack) character, it seems useful only for creating multiline macros.

In the C++ standard, the preprocessor removes any source-file newline immediately following a \ character. A macro ends with the next newline character, but sometimes you want a macro to expand to something that would normally fill several lines (an if-else statement, a case statement, or a structure definition). In these cases, you need to terminate each line in the macro definition with a \ character.

Your development environment probably contains some of these macros, a regular expression search on your include directories will probably reveal many uses of this.
 
MonkeyShave said:
If anybody is curious about the actual purpose of the \ (backwhack) character, it seems useful only for creating multiline macros.

Wow you can not be more wrong.

The backslash is an escaping character. Used when one wants a literal " or \. It's also used to enter certain characters, \xa (bell), \0 (null character), and so forth.
 
doh said:
Wow you can not be more wrong.

The backslash is an escaping character. Used when one wants a literal " or \. It's also used to enter certain characters, \xa (bell), \0 (null character), and so forth.
Sorry, I was referring to it only in the context of this thread: i.e. that the preprocessor removes the sequence backslash-newline. The only motive I can discern for this behavior is to enable "multiline" macro definitions. One could abuse it for other purposes, for example extending a // comment, but I doubt that such use inspired the behavior.
 
doh said:
Wow you can not be more wrong.
What's it matter, right or wrong? What matters is that someone's learnin' something.
 
mikeblas said:
That's odd. When I view the thread, my Post #3 (suggesting adjacent concatenation) is ahead of BillLeeLee's Post #4 (suggesting backwhack). It's only by a minute, but Post #3 is certainly ahead of your Post #6.

Odd how the title (Post #0, as it were) states "C" instead of "C++", as your "Post #3" suggests . . .
 
TheSpook said:
Odd how the title (Post #0, as it were) states "C" instead of "C++", as your "Post #3" suggests . . .
Oh, I see. Sorry about the typo.
 
Back
Top