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.
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, ...
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.
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.
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 !!!!!!!!!
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.
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.
Hmmmmm. Does endl take up more or less memory than a newline character?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'
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.
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.
As far as I know, "\n" is considered a string, but '\n' is only a character to it takes less memory
none...
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:
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.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.
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
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).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'
Your code is incorrect here. You cannot modify a string literal. You would need to do: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.
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.
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