This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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
  • It is interesting to note that the Keil C51 does have an input buffer that needs to be cleared.

    http://keil.com/support/docs/2017.htm

  • the Keil C51 does have an input buffer that needs to be cleared.

    That's an over-interpretaion of that support note you found. Yes, it does have a buffer, which you can clear if you want to. But by no means does that imply it needs to be cleared. In a nutshell: if you feel a need to clear stdin, you've quite invariably done something wrong before, i.e. you're now trying to fix symptoms instead of treating the disease.

    Basically, scanf() directly into any other format but strings is hardly ever an appropriate means of reading input from a non-deterministic source, e.g. a human operator. I.e. if you don't know for sure that the input will exactly match your scanf() format, every time, then scanf() is the wrong function to use. As I already indicated, error-prone input pretty much has to be read as a string, which you then sscanf().

  • Hans,

    That's an over-interpretaion of that support note you found.
    Yes, it does have a buffer, which you can clear if you want to.
    But by no means does that imply it needs to be cleared

    I think maybe you misinterpreted my post. You need to clear it only (on the C51 scanf) if unwanted characters appear was my intended meaning. I thought that was clear from the context of the thread but maybe not. I certainly was not suggesting that it should be done as a matter-of-course. :)