Developer Forums relocated!

Need help with Apple Developer tools and technologies? Want to share information with other developers and Apple engineers? Visit Developer Forums at Apple.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

printf() statement on multiple lines

Alright I'm missing something really obvious here. I have a simply printf() statement that spans multiple lines, for example:


printf(" text text text text text text text?
more text more text more text
even more text even more text");


Why am I getting errors here? If I combine the lines into one line, then the code runs fine, but when I separate them as above, I get errors. As far as I knew, it took a semicolon to mark off the end of a line of code in C, but obviously I'm missing something. Can someone help? Thanks.

MacBook, Mac OS X (10.5.8)

Posted on Oct 12, 2009 1:03 AM

Reply
Question marked as Best reply

Posted on Oct 12, 2009 1:55 AM

A string literal can't have embedded, unescaped new lines. The easiest way to break up your string literal is like this:

printf(
"string1"
"string2"
"string3",
arg1, arg2, arg3, arg4
);

The above works because the compiler combines consecutive string literals.

- Ray
6 replies

Oct 12, 2009 2:02 AM in response to RayNewbie

Thanks, Ray. I'm still a little confused though. I didn't see any formatting characters in your example, but are you saying I should do this:


printf("%s%s%s", " text text text text text text text? ",
"more text more text more text ",
"even more text even more text");


I know this will work. Is this what I need to do, or were you suggesting another way?

Oct 12, 2009 2:38 AM in response to Tron55555

Well your example is just a series of comma separated strings. The point is that you can break any string into multiple lines by quoting each fragment as a separate string literal. The compiler will then combine them into one string literal as if you had typed the entire string on one line.

I guess my example wasn't clear because I represented the format string as <string1> <string2>, etc. Here's a more explicit example:

printf( "Here is the digit for one: %d "
"Here is the digit for two: %d "
"Here is the digit for three: %d "
"Here is the Gettysburg Address: %s", // <-- this comma ends the formatting string
1, 2, 3, "Four score and seven years"
" ago our fathers brought forth on"
" this continent a new nation, conceived"
" in Liberty, and dedicated to the"
" proposition that all men are created"
" equal."
);

Be sure to notice that there are no commas separating the four string literals that comprise the format string. Those four lines will be compiled as one string literal.

- Ray

Oct 12, 2009 2:44 AM in response to RayNewbie

Yeah, we must have sent our responses at the exact same time. Anyways, I think the first post was plenty clear -- I was just being thick. However, I'm glad you put up the second one, because it was a good show of what all can be done with printf(). So the formatting string (the first argument) is basically just split up into separate strings without commas to separate them, and then any arguments to correspond to formatting specifiers go after that (which, if they are string arguments, can be split up in the same way). That's good to know. Thanks again.

printf() statement on multiple lines

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.