473,503 Members | 939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fflush(stdout);

Consider the following code:

#include <stdio.h>
int main( void )
{
printf("Hello");
fflush(stdout);
return 0;
}

fflush(stdout);
Is there a need for the above statement?

If so why do we need it?

Thanks in advance for the reply

Apr 7 '07 #1
6 80549
In article <11**********************@q75g2000hsh.googlegroups .com>,
Rajesh S R <SR**********@gmail.comwrote:
>Consider the following code:
>#include <stdio.h>
int main( void )
{
printf("Hello");
fflush(stdout);
return 0;
}
>fflush(stdout);
Is there a need for the above statement?
>If so why do we need it?
There is an automatic fflush() of all writable streams when
the program exits on any hosted environment. (The rules are different
for Freestanding environments.) Therefor in the program above,
the fflush does not add anything.

The program given has a portability problem: the output to stdout
does not finish with a \n. Implementations are permitted to drop
any terminal partial line on text streams. The presence of the
explicit fflush() does not affect this behaviour.

Generally speaking, fflush() has two uses:

1) it releases the current contents of the output buffer to the system
for whatever processing the system has for it. This is useful if
something (or some-one) is waiting for the output. For example if the
program was producing only a few lines of output every tens of minutes,
the user probably would prefer not to wait until the buffer fills up
(8Kb buffers are common) to see what has been happening. And when you
start gettting into interprocess communictions (outside the scope of C
itself), releasing current results for processing can be crucial to
proper operation;

1b) As a subset of the above: releasing an input prompt to the user
just before expecting input can be fairly important to the user;

2) In cases where you are updating a stream (opened with 'r+' or 'rb+'
or 'a+' or 'ab+' modes), flushing written output data before starting
to read from the stream is mandatory. In such an instance,
fflush() itself does not necessarily have to appear: fseek() will
also trigger the necessary flushing.
--
Programming is what happens while you're busy making other plans.
Apr 7 '07 #2
Rajesh S R wrote:
Consider the following code:

#include <stdio.h>
int main( void )
{
printf("Hello");
fflush(stdout);
return 0;
}

fflush(stdout);
Is there a need for the above statement?
In the particular case you show, no. That's because there is no more
more algorithm essentially after the printf(). Exiting the program
flushes all output streams, so you get the effect of the fflush()
implicitly.

Brian
Apr 7 '07 #3
Thanks for your reply.
That's because there is no more algorithm essentially after the printf().
Can u explain this statement a little more?

Apr 7 '07 #4

"Rajesh S R" <SR**********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
Thanks for your reply.
>That's because there is no more algorithm essentially after the printf().

Can u explain this statement a little more?
He means that after the 'printf()' the program doesn't
do anything else before it terminates, so there (theoretically)
should be no need to force output buffers to flush. But
keep in mind Walter's cautions about portability.

-Mike
Apr 7 '07 #5
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
[...]
The program given has a portability problem: the output to stdout
does not finish with a \n. Implementations are permitted to drop
any terminal partial line on text streams. The presence of the
explicit fflush() does not affect this behaviour.
Theoretically, it's worse than that. All the standard says about this
is (C99 7.19.2p2):

Whether the last line requires a terminating new-line character is
implementation-defined.

It doesn't define what happens if a terminating new-line character
isn't provided. Since the standard doesn't define the behavior, the
behavior is undefined.

Realistically, I'd be very surprised if the result were anything other
than:

(a) You get a text file whose last line just doesn't have a
trailing new-line character; or

(b) The system implicitly adds a new-line character for you; or

(c) The incomplete last line is discarded.

(I'm assuming here that stdout is written to a file.)

But it's possible, as far as the standard is concerned, for the
resulting text file to be ill-formed, and for later attempts to open
it to fail.

And if you're writing to a terminal or emulator, failing to terminate
the last line of output with a new-line can mess up the displayed
output, possibly causing the last line to be overwritten.

The bottom line: don't do that. (Yeah, the "bottom line".)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 8 '07 #6
"Rajesh S R" <SR**********@gmail.comha scritto nel messaggio
news:11**********************@q75g2000hsh.googlegr oups.com...
Consider the following code:

#include <stdio.h>
int main( void )
{
printf("Hello");
fflush(stdout);
return 0;
}

fflush(stdout);
Is there a need for the above statement?
*Here*, this is a silly thing to do, because of that others have said. The
very least bad thing that could happen (unless the compiler makes a program
which automatically adds a newline at the end) is having the command prompt
on the same line of the output (eg. HelloC:\or
Helloarmy1987@army1987-laptop:~$ or whatever).

Anyway, if you're going to use that in a longer program, this forces the
output to be shown immediately (this is only required when newlines are
printed and stdout is interactive). This is useful, e.g., if you need to
have input on the same line of the prompt. E.g.

fputs("Enter word to search: ", stdout);
fflush(stdout);
fgets(word, WORD_LENGTH+1, stdin);

without the second statement, it might not display the prompt until later.
Apr 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6064
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
3
10133
by: Fernando Arbeiza | last post by:
Hi: I need some clarification about a code like this: printf("%s", "a string with NO trailing newline"); scanf("%d", &i); Regarding if a fflush() of the standard output is needed or not. I...
1
2439
by: bine | last post by:
Sorry, I found a lot of old stuff, even kind of flamewars about good+bad style whatsoever.. I simply want any hint that works! I try to migrate older stuff from NT and newer stuff from...
5
2171
by: baumann | last post by:
hi all, when fflush function must be called? and why? thanks baumann@pan
0
1672
by: Chuck Gantz | last post by:
I have written two programs to test some stdin/stdout redirection ideas to eventually test a legacy console application writtn in C. One of the programs is a simple console app with some...
20
7244
by: David Mathog | last post by:
A program of mine writes to a tape unit. Output can be either through stdout or through a file opened with fopen(). When all the data is transferred to tape the program needs to close the output...
14
6209
by: asit | last post by:
please fix the bugs...?? #include <stdio.h> int main() { int i; char j; printf("Enter any number ...(1 or 2) : "); scanf("%d",&i);
1
6302
Siddarth777
by: Siddarth777 | last post by:
many times i encountered these two functions,especially i have used them in socket programming in unix environment but i just have a vague idea regarding functionality of those two functions THE...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.