Subject:
|
Re: how does a line end?
|
Newsgroups:
|
lugnet.cad
|
Date:
|
Sat, 7 Apr 2007 13:25:40 GMT
|
Viewed:
|
1724 times
|
| |
| |
In lugnet.cad, Tore Eriksson wrote:
|
In lugnet.cad, Chris Phillips wrote:
|
It is very easy for a programmer (regardless of the OS they are using) to
create programs that do not use proper line breaks. If you doubt it,
answer this quiz without using any reference materials:
1. Which of the following is "correct"?
a. printf("Hello, world!\r");
b. printf("Hello, world!\n");
c. printf("Hello, world!\r\n");
d. printf("Hello, world!\n\r");
2. In each of the above cases, what byte sequence will appear in the output stream?
|
1. I always use alt. b. printf(Hello, world!n); But as a self-made hobby
programmer not knowing all the tweeks and geeks, I wouldnt be surprised if
the interpretation of this may vary from one IDE to another, or if there is
an .ini file or global veraible somewhere that sets the output of n.
2. As I always use n, I dont know the output of the other alternatives. n
gives the sequence 13\10, or 0x0C 0x0A in hexadecimals from Borland TC++
v3.0.
|
Slight correction - 0x0D 0x0A
And this is kind of my point. The string you pass to printf() has \n only,
which is only one byte. Offhand I dont remember if this is because of the
Borland libraries, the operating system, or the fact that stdout is being
written in text mode instead of binary mode. The parameters that are passed
to fopen() can affect the way the output stream is written, causing \n to be
expanded into a 2-byte sequence. This platform-dependent behavior is why I
always open files in binary mode, even if they are supposed to be ASCII text
files.
But does this happen if you run the same source code through GCC on a linux box?
Probably not. So even if the programmer knows how their code will behave on
their development machine, it is very difficult to ensure that it is properly
written for every platform upon which it may eventually be used.
|
There was a Swedish finiance minister that said its safer to have both a
belt and suspenders. I think the optimal fool-proof way to go is to make
software as liberal as possible when it comes to input, but conservative on
output. Make it able to read Macs special LF but write strictly CR+LF.
|
Agreed 100%
By the way, the slashes disappeared from your reply because of the FTX
translation. To get \n you must type \\n in your post.
|
|
Message has 1 Reply: | | Re: how does a line end?
|
| (...) Aaah, that's one of my most common mistakes. C is the third letter, so I think of it as 13 and not 12. (...) At last, someone agrees with me. :) There will still be a theoretical risk that problems will occur, "anything that can go wrong will (...) (18 years ago, 7-Apr-07, to lugnet.cad)
|
Message is in Reply To:
| | Re: how does a line end?
|
| (...) 1. I always use alt. b. "printf("Hello, world!n");" But as a self-made hobby programmer not knowing all the tweeks and geeks, I wouldn't be surprised if the interpretation of this may vary from one IDE to another, or if there is an .ini file (...) (18 years ago, 7-Apr-07, to lugnet.cad, FTX)
|
24 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|