To my horror, i just realised fflush(stdin) is suppose to have undefined behaviour. Can someone tell a proper alternative to remove unwanted characters in the input stream?
To my horror, i just realised fflush(stdin) is suppose to have undefined behaviour. Can someone tell a proper alternative to remove unwanted characters in the input stream?
One way:
Another way:Code:int c; while ( ( c = getchar() ) != EOF && c != '\n' ) ;
Another variation:Code:scanf ( "%*[^\n]" ); scanf ( "%*c" );
Of course, if you find yourself doing this a lot you should wonder why your input constructs aren't being used more elegantly.Code:scanf ( "%*[^\n]" ); getchar();
My best code is written with the delete key.
The problem pretty much goes away once you stop using scanf() for reading input and always use fgets(). fgets() is far more predictable about how much data it will read from a stream.Code:int ch; while ( (ch=getchar()) != EOF && ch != '\n' ) { /* nothing */ } or more tersely while ( (ch=getchar()) != EOF && ch != '\n' );
So
Would becomeCode:int myint; scanf("%d",&myint); // some hacky flush
Code:int myint; char buff[BUFSIZ]; fgets( buff, sizeof buff, stdin ); sscanf( buff, "%d", &myint );
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
sorry, but i dont understand why do u need to compare the character with '\n' and eof. why not just eof? since basically, u want to flush everything in the stream?Code:int c; while ( ( c = getchar() ) != EOF && c != '\n' );
And what this mean?
i understand %[^\n] is used to match anything except the newline, but what is * for?Code:scanf ( "%*[^\n]" );
>since basically, u want to flush everything in the stream?
When a program asks you for input, do you use the enter key or the EOF control combination to end your input? Chances are you've done both, so we test for both so that a loop doesn't get hung waiting for EOF when your input was terminated with a newline.
>but what is * for?
It means throw away whatever is read. You don't need it since it's supposedly garbage, so why assign it to valuable memory?
My best code is written with the delete key.
Prelude.
Is there a chance of catching Ctrl+Z or F6 (EOF) using a signal handler?
signal(?, func);
R.I.P C89
I don't believe there is, EOF is passed to the application via a read call of some sort (fgets etc).Originally posted by c99
Prelude.
Is there a chance of catching Ctrl+Z or F6 (EOF) using a signal handler?
signal(?, func);
Out of interest, to see what signals are being sent by what action, you can always try something like this to trap every signal and go from there:
Code:#include <stdio.h> #include <signal.h> void handler(int sig) { printf ("Got signal %d\n", sig); } int main(void) { int i; char buf[BUFSIZ]; for (i = 0; i < NSIG; i++) { signal(i, handler); } fgets(buf, sizeof(buf), stdin); return(0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer.
Thanks, seems I can only manage to raise one signal from kbd action, SIGINT (Ctrl+C).
R.I.P C89
I don't believe it is correct to call printf (or most other standard library functions) in a signal handler that is invoked asynchronously.Originally posted by Hammer
Out of interest, to see what signals are being sent by what action, you can always try something like this to trap every signal and go from there:
Code:#include <stdio.h> #include <signal.h> void handler(int sig) { printf ("Got signal %d\n", sig); } int main(void) { int i; char buf[BUFSIZ]; for (i = 0; i < NSIG; i++) { signal(i, handler); } fgets(buf, sizeof(buf), stdin); return(0); }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Ok, now i understand how to flush properly. Thanks guys.
sorry for the, most likely, dumb question but is there any particular reason to use an int and not a char there?Code:int c; while ( ( c = getchar() ) != EOF && c != '\n' );