Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
The above works because the compiler combines consecutive string literals.
- Ray
0
Tron55555·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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?
0
Tron55555·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Nevermind, Ray -- I think I understand. You mean like this:
printf(" text text text text text text text? " "more text more text more text " "even more text even more text");
That seems to work well. Thank your for your answer.
0
RayNewbie·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
0
RayNewbie·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Yup, you've got it now. I hadn't seen your correction before I posted the additional example. - R
0
Tron55555·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.