LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   GCC Multiple line string problem! (https://www.linuxquestions.org/questions/programming-9/gcc-multiple-line-string-problem-497236/)

Shioni 10-31-2006 10:39 AM

GCC Multiple line string problem!
 
Hi!
How can I tell GCC that I'm using multiple lines in string, without getting syntax error?!:
Code:

char buffer[] = "FIRST LINE\N
SECOND LINE\N
THIRD LINE\N"

instead of:
Code:

char buffer[] = "FIRST LINE\NSECONDLINE\NTHIRDLINE";
Thank you!

Penguin of Wonder 10-31-2006 10:42 AM

Probably because the first one dosen't have a ";" after it and the second one does.

Penguin of Wonder 10-31-2006 10:47 AM

And after a bit of fiddling,

char buffer[] = "FIRST LINE\n"
"SECOND LINE\n"
"THIRD LINE\n";

Shioni 10-31-2006 01:13 PM

That worked, thanks alot man!

exvor 10-31-2006 01:50 PM

Code:


char buffer[] = "FIRST LINE\n"
"SECOND LINE\n"
"THIRD LINE\n";

/* could also be written */

char *buffer = "FIRST LINE\n"
"SECOND LINE\n"
"THIRD LINE\n";

If im correct in thinking here this would be a bad buffer as this data gets put into protected memory and cannot be modified.

mjones490 11-01-2006 08:52 AM

IIRC, you can also append each line with a backslash, signaling the following CRLF to be taken litterally and added to the string, like so:
Code:

char buffer[] = "FIRST LINE\
SECOND LINE\
THIRD LINE\n";



All times are GMT -5. The time now is 10:43 PM.