tags:

views:

226

answers:

8

i want to ascii value of '\n',

+13  A: 

The C escape code "\n" is usually 10 (decimal), or 0xa (hex). More information in various places - this was the first table I happened to find.

The ASCII line feed character is always 10... but whether "\n" means "ASCII line feed character" or not in your particular C compiler isn't guaranteed.

Note that even when the escape sequence "\n" means ASCII 10, some file writing routines convert "\n" into the platform default line terminator when you're writing the data. That doesn't affect what the data is in memory though.

EDIT: (I've edited the above too, slightly) Paul R points out this section from the Wikipedia newline entry:

The C programming language provides the escape sequences '\n' (newline) and '\r' (carriage return). However, these are not required to be equivalent to the ASCII LF and CR control characters. The C standard only guarantees two things:

  • Each of these escape sequences maps to a unique implementation-defined number that can be stored in a single char value.
  • When writing a file in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one character. When reading in text mode, the native newline sequence is translated back to '\n'. In binary mode no translation is performed, and the internal representation produced by '\n' is output directly.

On Unix platforms, where C originated, the native newline sequence is ASCII LF (0x0A), so '\n' was simply defined to be that value. With the internal and external representation being identical, the translation performed in text mode is a no-op, and text mode and binary mode behave the same. This has caused many programmers who developed their software on Unix systems simply to ignore the distinction completely, resulting in code that is not portable to different platforms.

Jon Skeet
You can verify with PHP - echo ord("\n");
El Yobo
I don't think it is guaranteed to have this value - e.g. on classic Mac OS it was 13.
Paul R
In Windows, particularly in a string, doesn't it usually mean `\x0d\x0a`? (CR (13), LF (10))?
cHao
@cHao No. That's \r\n.
Noon Silk
@Paul @cHao: See my edit.
Jon Skeet
@Paul: Max OS used \r (13) as newline. I'm however not sure if C compilers for Mac OS would translate \n to \r as well. I'd guess they would have to for portability.
Christoffer Hammarström
@Jon Skeet: some good info on this Wikipedia entry: http://en.wikipedia.org/wiki/Newline - not gospel, I know, but see the section where it reads: *The C programming language provides the escape sequences '\n' (newline) and '\r' (carriage return). However, these are not required to be equivalent to the ASCII LF and CR control characters. The C standard only guarantees two things...*
Paul R
@Christopher: they’re not conforming if they do, and no compiler that I know does this. `'\r'` != `\n`. Notice that accessing a file for read and write access in text mode will generally perform that conversion – but that’s something else entirely from saying that compilers convert every occurrence of a character in the code.
Konrad Rudolph
@Paul: Good catch. I've edited my answer appropriately.
Jon Skeet
A: 

Refer the complete list

Sri Kumar
Those "characters" in 0-31 are actually not part of ASCII. I believe this is one of IBM's character encodings, perhaps [Code page 437](http://en.wikipedia.org/wiki/Code_page_437).
Christoffer Hammarström
`\n` isn't mentioned anywhere in that list, just things like "line feed", "form feed", ... If the OP doesn't know what '\n' is then this table might not help much.
Mark Byers
+2  A: 

Here you can get the ascii values.

Praveen S
That page is rather misleading, as it does not say that that "extended ASCII" chart is actually [Code page 437](http://en.wikipedia.org/wiki/Code_page_437), and it is highly debatable whether it is the most popular 8-bit extension of ASCII.
Christoffer Hammarström
+4  A: 

The value of '\n' is usually 10, which is the ASCII value of the line feed character.

When you're programming it is often a better idea to write what you want to express rather than researching the value offline and then hardcoding it into your program.

Instead of:

if (x == 10) { ... }

Write:

if (x == '\n') { ... } 

The advantages of this are:

  • People reading your code will understand what you are trying to do without having to research which character has ASCII value 10.
  • If on your system that character has another value the code should hopefully still work.
Mark Byers
Nitpick: the ASCII values (!) of `"\n"` are 10, 0. ;-)
Konrad Rudolph
@Konrad Rudolph: Thanks, fixed.
Mark Byers
A: 

its should be 10.

-1: It's *usually* 10, but this is not guaranteed and you shouldn't make any assumptions about the value of \n if you want your code to be portable.
Paul R
Thanks paul for clarify me
A: 

It is 0x0A in HEX, and 10 in decimal.

More details check here : Newline.

You can check it yourself also :

int _tmain(int argc, _TCHAR* argv[])
{
    char ch = '\n';

    printf("The ACII value of '\\n' is - %d\n", ch);

    return 0;
}
Incognito
-1: It's *usually* 10, but this is not guaranteed and you shouldn't make any assumptions about the value of \n if you want your code to be portable.
Paul R
+1  A: 
#include<stdlib.h>
#include<stdio.h>

int main(int argc,char** argv)
    {
    int c='\n';
    printf("'%c' is %i\n",c,c);
    getchar();
    return 0;
    }

Is it really that hard to run this through gcc?

David X
A: 

The ASCII value of \n is 10. There's no "usually" about it. C does not require the character encoding to be ASCII or an ASCII superset, but if it is ASCII or an ASCII superset (like Latin-1 or UTF-8) then '\n'==10.

R..