writing a Multi-Line string

This is a discussion on writing a Multi-Line string within the C Programming forums, part of the General Programming Boards category; What is the best way to write a few lines of strings together? i.e. Line2 Line2 Line3 I attempted it ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    writing a Multi-Line string

    What is the best way to write a few lines of strings together?

    i.e.

    Line2
    Line2
    Line3


    I attempted it this way but it doesn't seem to work.

    Code:
    printf ( "Line1" );
    printf ( "Line2" );
    printf ( "Line3" );

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    printf("Line1\nLine2\nLine3\n");
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    You need to account for the newlines, e.g.,
    Code:
    puts("Line1\nLine2\nLine3");
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Lemme guess: "Line1Line2Line3"

    printf doesn't add a newline('\n'). You have to include it, or them:
    Code:
    printf ( "Line1\n" );
    printf ( "Line2\n" );
    printf ( "Line3\n" );
        
        or
    
    printf ( "Line1\nLine2\nLine3\n" );
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a string of hex values from command line
    By peeweearies in forum C Programming
    Replies: 7
    Last Post: 02-24-2009, 01:25 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21