473,657 Members | 2,423 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 80584
In article <11************ **********@q75g 2000hsh.googleg roups.com>,
Rajesh S R <SR**********@g mail.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**********@g mail.comwrote in message
news:11******** **************@ n76g2000hsh.goo glegroups.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.nr c-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_Keit h) 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**********@g mail.comha scritto nel messaggio
news:11******** **************@ q75g2000hsh.goo glegroups.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@a rmy1987-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
6088
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
3
10145
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 think the relevant portions of the standard are:
1
2447
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 Linux/AIX to run on Windows2003 as well. I use the Open Watcom C32 Optimizing Compiler Version 1.3 Portions Copyright (c) 1984-2002 Sybase, Inc. All Rights Reserved.
5
2180
by: baumann | last post by:
hi all, when fflush function must be called? and why? thanks baumann@pan
0
1686
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 Console::WriteLine's and printf to write to the console, and gets to read from stdin. This program works correctly when run standalone. The second program is a small Windows form that launches the first app and redirects stdin and stdout to itself so the...
20
7280
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 stream so that the tape driver will write a filemark on the tape. Otherwise multiple clumps of data saved to tape would all appear to be one big file on the tape. When the tape unit device was explicitly opened with fopen() that's possible:...
14
6228
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
6315
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 IDEA WHICH AM HAVING IS: they are used to clear input and output buffer but am not having a faintest idea regarding how they actually work please help me thanks a lot in advance
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.