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