scanf behaviour

R

Richard Heathfield

(e-mail address removed) said:
Richard , with all respect

U haven't provided any reference for your claim that fflush( stdin ) is
undefined, so for me
they are still only your words and not "The Standard"

You mean I haven't quoted this often enough before in comp.lang.c? Okay,
here we go again:

4.9.5.2 The fflush function

Synopsis

#include <stdio.h>
int fflush(FILE *stream);

Description

If stream points to an output stream or an update stream in which
the most recent operation was output, the fflush function causes any
unwritten data for that stream to be delivered to the host environment
to be written to the file; otherwise, the behavior is undefined.

If stream is a null pointer, the fflush function performs this
flushing action on all streams for which the behavior is defined
above.


(C99 contains similar wording.)
Read this, for example :

<man page snipped>

Man pages don't define the language.
 
R

Richard Heathfield

(e-mail address removed) said:
Thanks for the reference

By the way, the usage of "fflush( stdin ) is quite common :

So is drinking too much. That doesn't make it a wise thing to do.
 
M

Mark McIntyre

Well you flush beer pumps to clear out the stale beer.

Beer pumps output beer, they don't suck it back in from your glass....

And while you /can/ flush your stomach after inputting beer, its
considered a impolite. :)
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

I've tested "fflush( stdin );" on both WinXP and SunOS boxes it works
without a glitch.

That means nothing. It may work only in debug mode, or only provided
there is no other file operation going on in another application, or
only on saturdays. Tomorrow, when you're doing a demonstration to your
boss, it may fail.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Thanks for the reference

By the way, the usage of "fflush( stdin ) is quite common :

so is stepping on landmines, in some countries. Does that make it a
good idea?

Google also has 1.5 million articles matching a search for "sh*t
soup".
Remember that a random plunge into the web does not equate to a fact!
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
I

Ian Collins

Mark said:
Beer pumps output beer, they don't suck it back in from your glass....
But they suck it from the barrel and flushing clears the pipes that feed
the pump, which makes them the pump's stdin :)
And while you /can/ flush your stomach after inputting beer, its
considered a impolite. :)

I'm sure the Romans would have disagreed, but they didn't have C compilers.
 
I

Ian Collins

Thanks for the reference

By the way, the usage of "fflush( stdin ) is quite common

Probably only on systems where the behaviour is defined and predates
standard C.
 
I

Ian Collins

Mark said:
That means nothing. It may work only in debug mode, or only provided
there is no other file operation going on in another application, or
only on saturdays. Tomorrow, when you're doing a demonstration to your
boss, it may fail.
I don't know about XP, but on Solaris fflush() on an input stream is
defined:

"Flushing an input stream discards any buffered input and
adjusts the file pointer such that the next input operation
accesses the byte after the last one read."

This non-standard behaviour probably goes back to pre-standard days and
should be avoided in portable code.
 
M

Mark McIntyre

But they suck it from the barrel and flushing clears the pipes that feed
the pump, which makes them the pump's stdin :)

It may surprise you to learn this, given the quality of most beer, but
you do *not* flush the pipecleaner out into the barrel.....

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
I

Ian Collins

Mark said:
It may surprise you to learn this, given the quality of most beer, but
you do *not* flush the pipecleaner out into the barrel.....
I'm not talking about cleaning the pipes, but pulling though to flush
stale beer before serving the first pint of the session, the stretched
analogy being discarding the stale characters from a stream.
 
R

Richard Heathfield

Ian Collins said:
I'm not talking about cleaning the pipes, but pulling though to flush
stale beer before serving the first pint of the session, the stretched
analogy being discarding the stale characters from a stream.

If you insist on pushing this analogy, it's actually equivalent to something
that you might find in start-up code, such as:

memset(_iobuf._bufdata, 0, sizeof _iobuf._bufdata);
_iobuf[_STDOUT]._bufpos=0;

i.e. making sure that no stale characters remain in the output buffer, where
they might get served to a file by mistake.

But in any case it's a lousy analogy.
 
R

Richard Heathfield

Ian Collins said:
Probably only on systems where the behaviour is defined and predates
standard C.

Do you know of any such systems? Note that C was standardised in 1989.
 
R

Richard Heathfield

Ian Collins said:

I don't know about XP, but on Solaris fflush() on an input stream is
defined:

But in C, it isn't, and we discuss C here, not Solaris.
This non-standard behaviour probably goes back to pre-standard days

Unlikely. Do you have evidence to support that suggestion?
and should be avoided in portable code.

Agreed.
 
I

Ian Collins

Richard said:
Ian Collins said:



I can't find any evidence to support your assertion. Can you?
It wasn't an assertion, just speculation as to why an unmentionable
operating system defined fflush() on an input stream.

SunOS 4 predates ANSI C and it supported the fflush() function. I've
lost my old man pages, so I can't prove the documented behaviour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top