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.