fflush(stdin), scanf and a space

B

barry

Recently I came across following program:

#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
printf("%d",a);
scanf(" %d",&a); // note the space b/w " and %
printf("%d",&a);

}

There was no problem with this program
but the moment I removed the space in second scanf, program was
exiting before taking input of a in second scanf.
I think it was due to buffered carriage return in stdin file stream
I inserted fflush(stdin) above second scanf to rectify it.
I have also read 2 days ago that fflush(stdin) is not allowed
according to C standard and has undefined behavior.

I was thinking about a work around for equivalent of fflush(stdin).

I want to know that whether above trick of leaving a space before
format string in scanf for clearing stdin stream is reliable or not?
 
S

Seebs

I have also read 2 days ago that fflush(stdin) is not allowed
according to C standard and has undefined behavior.
Yup.

I was thinking about a work around for equivalent of fflush(stdin).

It's pretty hard to define.

Start at the top: What exactly do you want "flushed" or "discarded"?
How do you know, looking at a character, whether or not you want it
to be discarded? Do you want to discard everything up to the next newline?

If you want to discard "everything which was in the buffer already", that's
not possible to do portably/safely. (I usually look at the BSDish "fpurge()"
for that.) However, it's also often a bad idea, because on many systems,
input could be a file, in which case you'd be discarding an entire disk
block's worth of data usually, and that's almost never what you want.

A typical solution:

while ((c = getchar()) != EOF && c != '\n')
;

This discards everything up to the next newline.

-s
 
F

Fred

Recently I came across following program:

#include<stdio.h>
int main()
{
        int a;
        scanf("%d",&a);
        printf("%d",a);
        scanf(" %d",&a);         // note the space b/w " and %
        printf("%d",&a);

}

There was no problem with this program
but the moment I removed the space in second scanf, program was
exiting before taking input of a in second scanf.
I think it was due to buffered carriage return in stdin file stream
I inserted fflush(stdin) above second scanf to rectify it.
I have also read 2 days ago that fflush(stdin) is not allowed
according to C standard and has undefined behavior.

I was thinking about a work around for equivalent of fflush(stdin).

I want to know that whether above trick of leaving a space before
format string in scanf for clearing stdin stream is reliable or not?

Either your compiler is broken, or what you posted is not what your
code
really looks like. Other than the missing "\n" in the printf
statements, and not checking for input errors, it ought to work.

The newline still in the input buffer is whitespace; the second scanf
should try to read a new integer, ignoring any leading whitespace.
 
E

Eric Sosman

Recently I came across following program:

#include<stdio.h>
int main()
{
int a;
scanf("%d",&a);
printf("%d",a);
scanf(" %d",&a); // note the space b/w " and %
printf("%d",&a);

}

There was no problem with this program
but the moment I removed the space in second scanf, program was
exiting before taking input of a in second scanf.
I think it was due to buffered carriage return in stdin file stream

I don't think so. The "%d" specifier, all by itself, will skip
any leading white space it finds, and in the default "C" locale all
of space, tab, newline, carriage return, form feed, and vertical tab
count as white space. The leading " " would skip exactly the same
set of characters, so its presence or absence should make no difference.

It might be useful to know (1) what the input was, (2) what values
the scanf() calls returned, and (3) what values were stored in `a'.
I inserted fflush(stdin) above second scanf to rectify it.

Bad idea.
I have also read 2 days ago that fflush(stdin) is not allowed
according to C standard and has undefined behavior.

Right. (Which is why using it was a bad idea.)
I was thinking about a work around for equivalent of fflush(stdin).

Any of

*((double*)314159) = 3.14159;
free("Goodbye, cruel world!");
"spelling erryr"[12] = 'o';
printf ("%s");

(and many others) are rough equivalents, as they all produce undefined
behavior.
I want to know that whether above trick of leaving a space before
format string in scanf for clearing stdin stream is reliable or not?

A white-space character (any white-space character) in the format
string will match and ignore zero or more white-space characters (any
white-space characters) in the input, reading until it finds something
non-white or runs out of things to read.

... but, as mentioned above, since plain "%d" skips leading white
space anyhow, that should have made no difference. More details,
please!
 
P

Peter Nilsson

barry said:
Recently I came across following program:

#include<stdio.h>
int main()
{
        int a;
        scanf("%d",&a);

You should check the return value of scanf.
        printf("%d",a);
        scanf(" %d",&a);         // note the space b/w " and %

The space is redundant as %d will ignore leading whitespace,
including new-lines anyway.
        printf("%d",&a);

Did you really mean to print the address?
}

There was no problem with this program
but the moment I removed the space in second scanf,
program was exiting before taking input of a in
second scanf.

I'm doubting this is the actual code.
I think it was due to buffered carriage return in stdin
file stream I inserted fflush(stdin) above second scanf
to rectify it.

All you did was invoke undefined behaviour since fflush()
is only defined for output streams.
I have also read 2 days ago that fflush(stdin) is not
allowed according to C standard and has undefined behavior.
Correct.

I was thinking about a work around for equivalent of
fflush(stdin).

First tell us what you think fflush(stdin) is supposed to
do.
I want to know that whether above trick of leaving a space
before format string in scanf for clearing stdin stream is
reliable or not?

As stated, it adds nothing.

If you want to read a line, use fgets and scan the string
using sscanf or strtoxxx.
 
E

Eric Sosman

[... something incoherent ...]
i'm like one horse i like freedom

You're not all that much like a horse. Horses are considerably
larger than people, usually walk on four legs rather than on two,
bear infants that can stand upright within a few hours of birth, and
so on. Few people viewing pictures of io_x and of a horse would have
difficulty in telling which was which, provided the horse's picture
showed the end with the head.
 

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

Similar Threads

fflush(stdin), scanf and a space 5
fflush and stdin 17
strange scanf 1
Function is not worked in C 2
Problem with scanf 7
Q for a source code in an exercise 1
fflush() 5
fpurge/fflush 21

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top