"\n" or '\n'

This is a discussion on "\n" or '\n' within the C++ Programming forums, part of the General Programming Boards category; whats the difference between "\n" and '\n' single quote versus double for new line character. they both do the same, ...

  1. #1
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    "\n" or '\n'

    whats the difference between "\n" and '\n'

    single quote versus double for new line character.
    they both do the same, but would one be used
    for a specific application ?

    thnx in advance.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Single quotes are for single characters where as double quotes are for strings. They may still be used for single characters, but single quotes may not be used for strings. Confused? Hopefully, this will help:
    Code:
    'A'	// valid
    "A"	// valid
    "foo bar"		// valid
    'foo bar'		// invalid
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User Spectrum48k's Avatar
    Join Date
    May 2002
    Posts
    66

    author cant makeup his mind ..

    that makes sence.
    this author cant make up his mind with new line character.
    i use the double quote. in the book though, in every example
    he seems to use one or the other. it can get frustrating when
    trying to debug something that isnt wrong to begin with, while the true error goes un-noticed

    thanx..
    its alive... its ALIVE... ITS...AL...IIIVE !!!!!!!!!

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    One thing my programming teacher said once about a very confusing description in our textbook was that it was probably written by a grad student. Just a possibility. Heck, I've heard some books even have void main in them. Can't trust everything you read.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Use '\n' in most cases. When using double quotes, it makes a null-terminated string, so "\n" actually takes up more space in memory than '\n'

    Single quotes can also be used for multiple characters, however, when you do, it does not produce a nullterminated string, it lines up the characters into an integer. The amount of characters has to be less than or equal to sizeof(int)

    For instance, you can do:

    int a = 'abcd';

    Which will store the ascii values of a, b, c, and d in a row in an int.
    Last edited by Polymorphic OOP; 01-23-2003 at 07:54 PM.

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Polymorphic OOP
    Use '\n' in most cases. When using double quotes, it makes a null-terminated string, so "\n" actually takes up more space in memory than '\n'
    Hmmmmm. Does endl take up more or less memory than a newline character?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    endl is an inline function which internally just outputs '\n' and then flushes the stream, so it actually will take up slightly more memory, however, it is generally equivalent in implementation and size to just doing

    << '\n' << flush;

    So if you want the stream to be flushed as well, just use endl, unless you were already in the middle of outputting a string, you might as well just tack on \n to the end of the string and then << flush afterwards, but the difference is so minute, it's trivial to make the distinction.

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    As far as I know, "\n" is considered a string, but '\n' is only a character to it takes less memory
    none...

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Enclosing a string in double quotes gives it an address in the resource section of the executable. The compiler converts it into a pointer to that string. Single quotes convert an ASCII character into it's code equivalent. Ie:

    Code:
    char *str="Hello World!";
    //The null-terminated string 'Hello World!' is stored in a part of the executable, and it's address will be placed in str.
    
    char str[1]='a';
    //'a' is converted into it's character code equivalent, 97, which is then stored in str.
    I believe that being able to enclose escape codes such as \n within single quotes is an added capability to make things easier for the programmer.

    EDIT: Actually, I just found that \n resolves to character code 10, so the rule does hold for it.
    Last edited by bennyandthejets; 01-24-2003 at 03:46 AM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    nag
    nag is offline
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    Originally posted by Polymorphic OOP
    Use '\n' in most cases. When using double quotes, it makes a null-terminated string, so "\n" actually takes up more space in memory than '\n'
    Yes it is true "\n" has a null terminated character at its end which is '\0' but '\n' is only converted to its ASCII value (which is 10).

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by bennyandthejets

    Code:
    char *str="Hello World!";
    //The null-terminated string 'Hello World!' is stored in a part of the executable, and it's address will be placed in str.
    
    char str[1]='a';
    //'a' is converted into it's character code equivalent, 97, which is then stored in str.
    Your code is incorrect here. You cannot modify a string literal. You would need to do:

    char str[] = "Hello World!";

    You cannot modify the string:

    char *str = "Hello World!";

    Now you could make str point to something else, but you can't change the individual characters in the afore mentioned string.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You misunderstand what I was trying to indicate in my post. Notice that both lines are separate declarations. They both declare 'str' to be a different type. I'll change it so it makes sense.

    Code:
    char *str1="Hello World!";
    char str2[1]='a';
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-06-2008, 01:43 PM
  2. differnce between endl and "\n"
    By BEN10 in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2008, 10:08 AM
  3. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 07:56 PM
  4. How to print "\n" or "\t"
    By Joelito in forum C Programming
    Replies: 1
    Last Post: 01-13-2007, 08:22 PM
  5. Sending "\n" as a string?
    By applejustworks in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2006, 06:37 PM

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