string on multiple lines in c

Sharky Forums


Results 1 to 8 of 8

Thread: string on multiple lines in c

  1. #1
    Goldfish
    Join Date
    Dec 2004
    Posts
    76

    string on multiple lines in c

    I want to make a long string in C using multiple lines.
    I try
    Code:
     "This is a long ...................\
                     ............... string"
    and the a unprintable character is displayed on the output for \
    how do I do this without using strcat?
    Last edited by xrax; 08-10-2005 at 12:34 PM.

  2. #2
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612
    The normal way of doing it is taking advantage of the fact that adjacent strings are concatenated by the preprocessor.

    "This is a long ..............."
    "................... string"

  3. #3
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612
    Just curious, what happens if you output something like this:

    "This is a long ...............\n .............string"

    Because that's what I imagine your string looks like, to the compiler. Do you get the same unprintable character?

  4. #4
    Goldfish
    Join Date
    Dec 2004
    Posts
    76
    okay I got macros and strings confused.

  5. #5
    Texan Dragon Moderator Galen of Edgewood's Avatar
    Join Date
    Sep 2000
    Location
    Ft Irwin, CA
    Posts
    5,602
    xrax, did you get it figured out?

    Remember, if you don't tell C to go to the next line, it'll continue to print on that same line until it can't anymore. The '\n' forces C to go to the next line.
    Dragon of the OC Crusaders

    Break the rules and you're snack food for this dragon...

  6. #6
    Goldfish
    Join Date
    Dec 2004
    Posts
    76
    yes, figured it out. The \ was telling the precompiler to put it on the same line but it was also printing a character in it's place. it should only be used in macros (#define)

  7. #7
    Great White Shark Vengance_01's Avatar
    Join Date
    Oct 2001
    Location
    Moreno valley,CA
    Posts
    5,911
    hum I am not sure what you meant, but you just wanted that be outputed to the screen. Code should look like this right?

    // Messing Around Chp 2

    #include <iostream>
    using namespace std;

    int main()

    {
    cout << "This is a long................... \n";
    cout << "....................... String" << endl;
    return 0;

    }
    Updated 12/27/05

    A64 Desktop Rig
    DC Opteron [email protected]
    1GB UTT@245FSB 2/2/2/7
    X1800XT 512MB @PE Speeds
    19" Viewsonic WS LCD
    2x160GB ATA HDs
    NEC 3550 DL-DVD Burner

    12.1 Ibook G3
    256MB Ram
    30GB

  8. #8
    Goldfish
    Join Date
    Dec 2004
    Posts
    76
    cout is not available in C and the << operator is a bit shift in C. plus that is 2 strings not one longer string. for macros (pre-processor #define) on multiple lines you add the \ to the end of a line to tell the preprocessor to go to next line. the problem was that the preprocessor was combining the 2 lines but including the \ in the string.
    Quote Originally Posted by "Strogian"
    The normal way of doing it is taking advantage of the fact that adjacent strings are concatenated by the preprocessor.

    "This is a long ..............."
    "................... string"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •