0

Consider the following code:

#include<stdio.h>

void main()
{
    int p = 0;
    char a = 0;
    while(p == 0)
    {
        p = scanf("%[abc]c",&a);
    }
    printf("%c",a);
}

I know if you enter a character except 'a', 'b', or 'c', value of the getting character from input would not be set into the variable a.

The problem is if you enter a value except the specified characters, the above loop never and never terminates. Apparently, after getting a not specified character in scanf, it will not get any character in the following loop iteration. Where is the origin of this problem?

OmG
  • 18,337
  • 10
  • 57
  • 90
  • 2
    Do you think that `[]` works as a kind of modifier to the `%c` conversion? If so, that's not how it works. – unwind Feb 01 '17 at 13:13
  • you can debug and see the behavior is as what I said, in the front side. – OmG Feb 01 '17 at 13:20

0 Answers0