scanf conflicting characters remain in the input stream

hello, there's a little problem with the scanf-function. My program

...
 scanf("%u",&A);
...
 
expects a unsigned integer. But when someone hits a unwanted character, scanf terminates. Any conflicting character remain in the input stream. How to clear the scanf-input-stream? Is there a known workaround for ARM7 ADuC7026)? In C51 you just
CLR ?C?charloaded
RET
But how on ARM7? Thanks for advice :-)

Parents
  • there's a little problem with the scanf-function

    Absolutely not. The only problem here is that you haven't learned enough about scanf() before you started using it.

    How to clear the scanf-input-stream?

    You don't, for two reasons. First, because there is no such thing as a scanf-input-stream. That stream is in no way special to scanf. It is, for all practical intents and purposes, the standard input stream, a.k.a. stdin. Second, because stdin cannot meaningfully be "cleared".

    The issue you've stumbled over is the reason that scanf() is very rarely used in production-grade C programs. They use fgets and ssscanf() instead.

Reply
  • there's a little problem with the scanf-function

    Absolutely not. The only problem here is that you haven't learned enough about scanf() before you started using it.

    How to clear the scanf-input-stream?

    You don't, for two reasons. First, because there is no such thing as a scanf-input-stream. That stream is in no way special to scanf. It is, for all practical intents and purposes, the standard input stream, a.k.a. stdin. Second, because stdin cannot meaningfully be "cleared".

    The issue you've stumbled over is the reason that scanf() is very rarely used in production-grade C programs. They use fgets and ssscanf() instead.

Children
More questions in this forum