PDA

You are currently viewing a search engine-friendly (archive) version of this page.

View Full Version : printf() vs. fprintf() ? in C


Tonker
March 21, 2001, 07:02 pm
Does anybody know the differences between printf() & fprintf() in terms of flushing the output buffer? I've heard somewhere that it is preferable to use one of them, but I can remember which it was, or why.

------------------

MikeFoster
March 22, 2001, 12:58 am
Hi Tonker,

I'm not sure I can precisely answer your question, but I can chat a little about it anyway.

As you know, the ANSI C I/O system is a buffered system. When you call fprintf(fp, "%s", s) then the stream will not be flushed until you call fclose(fp), right?

Now, I think the question is really this: is this call: printf("%s", s) identical to this call: fprintf(stdout, "%s", s)? I don't think so. printf() knows its writing to stdout, so it can flush after each call - otherwise there would be times when a call to printf() did not display anything on the screen because it was buffered and awaiting a flush - sound good? However, fprintf() only knows that its writing to a stream - it should not care at all what device is associated with that stream. fprintf() leaves it up to the programmer to eventually call fclose(), when any flushing will be done, if needed.

Yet, I think I'm missing something here. Am I saying that this call fprintf(stdout,"%s",s) may not cause text to display on the console until the program exits (when stdout will be closed)? I'm not sure. Perhaps the console stream is not buffered at all - is that right? It may be that this call: printf("%s", s) is in fact identical to this call: fprintf(stdout, "%s", s) provided that stdout is associated with the console.

Now I'm confused... I've gotta dig a little deeper into this.

------------------
Try these HelpFromTechs.com Web Development Resources:
Reference Links (http://www.helpfromtechs.com/ubb/Forum8/HTML/000054.html)*|*User Authentication (http://www.helpfromtechs.com/ubb/Forum8/HTML/000071.html)*|*HTML Editors (http://www.helpfromtechs.com/ubb/Forum8/HTML/000030.html)*|*Perl/CGI (http://www.helpfromtechs.com/ubb/Forum8/HTML/000016.html)*|*Embedding Music (http://www.helpfromtechs.com/ubb/Forum8/HTML/000067.html)

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">This message has been edited by MikeFoster on March 21, 2001 at 10:00 PM</font>

Tonker
March 23, 2001, 11:33 am
Thanks for the response Mike! What you said seems to have jogged my memory. http://www.helpfromtechs.com/ubb/smilies/smile.gif

printf( "%s", str ) is probably what I want to be using for my output to stdout. I guess where the confusion arose was in regards to error messages. From what you've said it looks like I want to be doing a fprintf( stderr, "%s\n", str ); fflush( stderr );

I think that makes sense. http://www.helpfromtechs.com/ubb/smilies/wink.gif

Thanks again!

------------------

MikeFoster
March 24, 2001, 03:58 am
I think stderr is unbuffered... I'll have to look that up.

------------------
Try these HelpFromTechs.com Web Development Resources:
Reference Links (http://www.helpfromtechs.com/ubb/Forum8/HTML/000054.html)*|*User Authentication (http://www.helpfromtechs.com/ubb/Forum8/HTML/000071.html)*|*HTML Editors (http://www.helpfromtechs.com/ubb/Forum8/HTML/000030.html)*|*Perl/CGI (http://www.helpfromtechs.com/ubb/Forum8/HTML/000016.html)*|*Embedding Music (http://www.helpfromtechs.com/ubb/Forum8/HTML/000067.html)