News for hackers. Stuff that matters.
Re:printf("goodbye, Dennis"); by pjscott on Friday October 21, 2011 @07:09PM (#3142131)
I am slightly bothered by the lack of a '\n'.
Re:printf("goodbye, Dennis"); by tmcb on Friday October 21, 2011 @07:13PM (#3142142)
You're not the only one.
Re:printf("goodbye, Dennis"); by 0x12 on Friday October 21, 2011 @08:17PM (#3142318)
Think of it as an open ending, there is still much more to come.
Re:printf("goodbye, Dennis"); by mmacaulay on Saturday October 22, 2011 @02:19PM (#3143994)
Yes, but what better metaphor for new beginnings than a "new line"? :-)
Re:printf("goodbye, Dennis"); by camperman on Sunday October 23, 2011 @03:24AM (#3145477)
As 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.

Re:printf("goodbye, Dennis"); by yxhuvud on Saturday October 22, 2011 @05:00AM (#3143204)
I'm more bothered by the lack of #include and abysmal function signature.
Re:printf("goodbye, Dennis"); by drdaeman on Saturday October 22, 2011 @10:45AM (#3143598)
My 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)#.22Hel... and I don't have any copy of the first edition.

Re:printf("goodbye, Dennis"); by keithpeter on Saturday October 22, 2011 @06:18AM (#3143286)
I'm glad you said that. I thought it was just me.
Re:printf("goodbye, Dennis"); by matthavener on Friday October 21, 2011 @07:41PM (#3142222)
Why?
Re:printf("goodbye, Dennis"); by mhartl on Friday October 21, 2011 @07:45PM (#3142230)
The 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");
Re:printf("goodbye, Dennis"); by Lukeas14 on Friday October 21, 2011 @07:46PM (#3142233)
"\n" is a linebreak which would guarantee the text is printed and place whatever output came afterwards on the next line.
Re:printf("goodbye, Dennis"); by derefr on Friday October 21, 2011 @07:52PM (#3142246)
Isn't the output stream at least guaranteed to be flushed once on completion of the program, though?
Re:printf("goodbye, Dennis"); by djcapelis on Friday October 21, 2011 @07:57PM (#3142258)
Yes, 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.

Re:printf("goodbye, Dennis"); by derefr on Friday October 21, 2011 @08:26PM (#3142341)
(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.)
Re:printf("goodbye, Dennis"); by getsat on Friday October 21, 2011 @10:11PM (#3142504)
Adding a newline doesn't flush anything. You'd want to call fflush() to do that.
Re:printf("goodbye, Dennis"); by bremac on Friday October 21, 2011 @10:30PM (#3142543)
printf 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;
  }
Re:printf("goodbye, Dennis"); by getsat on Saturday October 22, 2011 @02:05PM (#3143964)
Yes. 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.

Re:printf("goodbye, Dennis"); by jimbokun on Saturday October 22, 2011 @07:33PM (#3144623)
"Also, using printf() for debugging? In 2011? I seriously hope you guys don't do this."

OK, I'll bite. I haven't done any serious straight up C programming in over a decade, and yes, I used a lot of printf for debugging back then. What's the best way to debug C programs now? (I imagine printf wasn't the best way to debug programs when I was writing C, either.) A different logging library? gdb?

Re:printf("goodbye, Dennis"); by getsat on Sunday October 23, 2011 @02:42AM (#3145409)
printf() is fine if you just want to see if something is being run (assuming, of course, you recognize the flushing caveat we're discussing).

If you're dumping printf()s all over the place to see exactly what flow your program is taking at runtime, you're better off just setting a breakpoint or two and using a debugger to quickly step through it.

Re:printf("goodbye, Dennis"); by bremac on Saturday October 22, 2011 @04:04PM (#3144206)
Adding 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.