fflush(stdin), scanf and a space

T

Tagore

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?
 
B

Ben Bacarisse

Tagore said:
Recently I came across following program:

#include<stdio.h>
int main()

int main(void) is better.
{
int a;
scanf("%d",&a);
printf("%d",a);
scanf(" %d",&a); // note the space b/w " and %
printf("%d",&a);

This is wrong (undefined behaviour). Have you re-typed? It is always
better to get the real code into your post if you can. You may not
see the output unless you end with a newline and it is better to
return 0; at the end of main (it is permitted to omit the return in
C99).
}

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.

Sounds odd. Care to name and shame the compiler/library that you are
using?
I think it was due to buffered carriage return in stdin file stream

It sounds like an error in the scanf implementation to me.
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.

Yes, it is undefined so that is not a solution. I'd suggest one but
if scanf is going wrong, it is very hard to guess what will fix it.
I was thinking about a work around for equivalent of fflush(stdin).

I don't see the need if leaving the space in there happens to work.
It should not make any difference, but since it does, why not just
leave it there?
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?

I don't know what you mean by "clearing" but you should be able to
rely on the specified behaviour of scanf: any white space in the
format causes scanf to read up to the next non-space character,
leaving it in the stream. This happens with %d anyway, which is why
"%d" and " %d" should be the same. If scanf is not doing what it
should then you are in a strange situation and it is hard to give
practical advice.
 
B

Ben Bacarisse

Andrew McMeikan said:
If it works it is probably a good way of doing it, I recently had
same problem with fgets

I don't think you had the same problem. The OP is describing
something that sounds like an error in scanf. They posted code that
has other errors so maybe the report is wrong but so far the only
information is that something is broken in scanf. If I remember your
code, the behaviour of the library functions was correct.
... and used following

void flushstdin(void){
if (!feof(stdin)) {
getc(stdin);
}
}

That does something else altogether! Inserting that between the scanf
calls in the OP's code it would cause +1-2 to be read as 1 and 2 rather
than 1 and -2. It seems a dangerous suggestion to throw away an input
character.
but for scanf the space seems neat.

Yes, it has its uses if scanf is doing what it should. However that
space should not matter in this particular case.
 
A

Andrew McMeikan

Tagore said:
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?

If it works it is probably a good way of doing it, I recently had
same problem with fgets and used following

void flushstdin(void){
if (!feof(stdin)) {
getc(stdin);
}
}
but for scanf the space seems neat.

cya, Andrew...
 
C

CBFalconer

Ben said:
.... snip ...

I don't know what you mean by "clearing" but you should be able
to rely on the specified behaviour of scanf: any white space in
the format causes scanf to read up to the next non-space
character, leaving it in the stream. This happens with %d
anyway, which is why "%d" and " %d" should be the same. If
scanf is not doing what it should then you are in a strange
situation and it is hard to give practical advice.

I rarely use scanf, but wouldn't " %d" REQUIRE a space before the
value, while "%d" will absorb all spaces before the value?
 
E

Eric Sosman

CBFalconer said:
Ben Bacarisse wrote:
... snip ...

I rarely use scanf, but wouldn't " %d" REQUIRE a space before the
value, while "%d" will absorb all spaces before the value?

No, the " " will match any number of white space input
characters, including zero. 7.19.6.2p5:

A directive composed of white-space character(s) is
executed by reading input up to the first non-white-space
character (which remains unread), or until no more
characters can be read.

If the very first character is non-white, the " " directive
is finished and the non-white character remains unread, ready
to be chewed over by the "%d".

In short, " %d" should behave identically to "%d", and I
am unable to explain the O.P.'s report in terms of the C Standard.
Either he's mis-reported, or his C library has a bug.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top