4

Now line separator is system dependent but while in a c program i use '\n' for line separator , it works fine whether i run it in windows or linux . why ??

In java we have to use %n as it is system dependent then how come we use '\n' in c for new line irrespective of os in which we run it ?

Number945
  • 4,631
  • 8
  • 45
  • 83
  • windows uses `\n\r` for newline (+carriage return). The `\r` is easily ignored, because it's a non-printing character – EOF Apr 05 '14 at 21:45
  • 3
    First, Java always uses the Unicode character set, even on EBCDIC machines, so no problem there. And for C/C++ `\n` translates to the machine's version of "newline", not necessarily 0x0A. – Hot Licks Apr 05 '14 at 21:46
  • @HotLicks , u mean to say that java is unable to translate \n to machine's version of "newline" while c compiler does it automatically ?? – Number945 Apr 05 '14 at 22:07
  • Actually, windows uses `\r\n` -- the carriage return always comes before the linefeed. – Chris Dodd Apr 05 '14 at 22:07
  • @BreakingBenjamin - I mean that Java implements `char` and `String` using UTF16, then translates from there to the machine codepage when writing data, while C operates (or at least might) in the machine codepage. – Hot Licks Apr 05 '14 at 22:22

2 Answers2

2

Here's what C99 standard has to say about it:

5.2.2 Character display semantics
[...]
2. Alphabetic escape sequences representing nongraphic characters in the execution
character set are intended to produce actions on display devices as follows:
[...]
\n (new line) Moves the active position to the initial position of the next line.

This means that if you print "\n", it has to be changed to "\r\n" on windows in order to fulfill this requirement. But this is only defined this way for display devices, not for files. There is no specification on how this (or any other whitespace) character should be represented in text-file, however:

5.2.2 Character display semantics
[...]
3. Each of these escape sequences shall produce a unique implementation-defined value
which can be stored in a single char object. The external representations in a text file
need not be identical to the internal representations, and are outside the scope of this
International Standard.

So the direct answer to your question is - it works as you would expect because the C standard does not say anything about binary representation of \n character when it's printed to the display device or stored in text-file. It does, however, precisely specify how it should behave when printed - it should move to the beginning of next line.

Krzysztof Adamski
  • 2,039
  • 11
  • 14
0

In C programs, you specify when you open the file whether it is text or binary -- when you open a file with fopen, you specify a mode string, which includes b if this is a binary file. When reading from and writing to text-mode files, the C library will automatically translate between '\n' characters and the system line separator.

In java, there is no analogous "text mode" that automatically translates for you, so to deal with line separators, you need to do it explicitly in your code.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • There definitely is a difference between text and binary file IO in Java. They use different classes. – Hot Licks Apr 05 '14 at 22:25
  • @HotLicks: those are different I/O managers -- they share the underlying File objects, which don't differentiate between text and binary and can be use for either. I guess it would have been possible for java to specify line-terminator translation when using text I/O, but they did not. – Chris Dodd Apr 05 '14 at 22:36
  • Yes, they share the same underlying File, but you don't do I/O directly to a File. This doesn't really differ from C/UNIX, other than the way you specify it. – Hot Licks Apr 05 '14 at 22:41
  • @HotLicks: It differs in that C does explicit translation between `\n` characters and the system line separator when in text mode and java does not. – Chris Dodd Apr 05 '14 at 22:54
  • I'm don't know what you're talking about (and I'm not sure you do either). When you do a `write` of `\n` to a Java Writer, on an EBCDIC system that will be translated into the appropriate line-end character. – Hot Licks Apr 06 '14 at 02:33