5 ms·
I am slightly bothered by the lack of a '\n'.
by pjscott 15y ago
I am slightly bothered by the lack of a '\n'.
- tmcb 15y agoYou're not the only one.
- matthavener 15y agoWhy?
- mhartl 15y agoThe code printf("goodbye, Dennis"); would give $ ./goodbye goodbye, Dennis$ What you want is to add a newline ('\n') at the end: printf("goodbye, Dennis\n"); Now you get $ ./goodbye goodbye, Dennis $ That's why the canonical "hello, world" program contains the line printf("hello, world\n");
- Lukeas14 15y ago"\n" is a linebreak which would guarantee the text is printed and place whatever output came afterwards on the next line.
- derefr 15y agoIsn't the output stream at least guaranteed to be flushed once on completion of the program, though?
- djcapelis 15y agoYes, it flushes completely outputting all characters. None of which contain a newline. Flushing doesn't automatically add newlines for you in C. It flushes the buffers you gave it, it doesn't make new stuff up to print out along with your buffers.
- derefr 15y ago(Right, I didn't mean to suggest it added a newline; just that adding a newline flushes the buffer, which was one half of the problem with missing a newline—but that if this is the only line of the program, this half of the problem is obviated.)
- getsat 15y agoAdding a newline doesn't flush anything. You'd want to call fflush() to do that.
- bremac 15y agoprintf prints to standard output, which is line-buffered by default when reading from a tty on UNIX variants. This can lead to strange problems for those who try to use it to track which parts of a program are being executed, as demonstrated by the following program with an infinite loop: #include <stdio.h> int main(int c, char **v) { printf("Hello\nJello"); while(1) ; return 0; }
- getsat 15y agoYes. You're not agreeing or disagreeing with me. Did you reply to the wrong post? Also, using printf() for debugging? In 2011? I seriously hope you guys don't do this.
- bremac 15y agoAdding a newline into a line buffer (which is a user-space buffer) flushes the buffered data up to and including the newline. fflush also flushes a user space buffer. Also, the "for those" implies I'm talking about someone else, not myself.
- yxhuvud 15y agoI'm more bothered by the lack of #include and abysmal function signature.
- drdaeman 15y agoMy first thought, too. Quite surprising, though, at least as Wikipedia claims¹, this (except for the lack of "\n") was the exact "hello world" code from the first edition of K&R. ____ ¹) http://en.wikipedia.org/wiki/C_(programming_language)#.22Hello.2C_world.22_example http://en.wikipedia.org/wiki/C_(programming_language)#.22Hel... and I don't have any copy of the first edition.
- keithpeter 15y agoI'm glad you said that. I thought it was just me.
- camperman 15y agoAs both programmer and journalist, I can guess what happened behind the scenes. The author probably originally put in the newline into his headline but was overruled by the editor who felt that it would have distracted too many non-technical people. You and I see the correctness of the \n but a non-programmer will just see it as strange noise or punctuation. He allowed him the C statement but kept it simplified - a good compromise I think.