none
split printf(" ") on multiple lines RRS feed

  • Question

  • 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?


    • Edited by LORD Revan Monday, July 29, 2019 4:49 AM grammatical error
    Monday, July 29, 2019 3:58 AM

Answers

  • 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 )", . . . );



    • Edited by Viorel_MVP Monday, July 29, 2019 4:33 AM
    • Marked as answer by LORD Revan Monday, July 29, 2019 4:42 AM
    Monday, July 29, 2019 4:25 AM

All replies

  • 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 )", . . . );



    • Edited by Viorel_MVP Monday, July 29, 2019 4:33 AM
    • Marked as answer by LORD Revan Monday, July 29, 2019 4:42 AM
    Monday, July 29, 2019 4:25 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.


    Monday, July 29, 2019 4:40 AM