Share via


split printf(" ") on multiple lines

Question

Monday, July 29, 2019 3:58 AM

hi everybody, i am a newbie. i was curious to know if there  is a way to split  printf("   long text ") on multiple lines without visual studio prompting an error?

i have a very  very long text that I need to print on screen and when I add the printf statement i must keep everything in a single line and scroll all the way to the right to reach the end of my text or split the text in many printf() if i want to avoid scrolling.

is there a way to ensure that visual studio code modify something like this

printf( " part1

part2

part3 ");

in printf("part 1 part 2 part3") during the compilation phase?

All replies (2)

Monday, July 29, 2019 4:25 AM âś…Answered | 1 vote

Try this:

printf( " part1 "
        " part2 "
        " part3 ", . . . );

or this:

printf( " part1 \
 part2 \
 part3 ", . . . );

The next variant inserts new-lines between parts:

printf( R"( part1 
 part2 
 part3 )", . . . );

Monday, July 29, 2019 4:40 AM

Hi LORD,

Welcome to MSDN forum.

AFAIK, there's two ways to broke a long printf statement into multiple lines:

One is what Viorel_ suggests, concatenate multiple strings together, one on each line.

And another way is to use a backslash as the last character, something in this format:

  printf("part1 \
part2 \
part3");

More details see this similar issue. Hope it helps.

Best Regards

Lance

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.