Scanf is not scanning %c character but skips the statement, why is that? [duplicate]

霸气de小男生 提交于 2019-11-27 09:10:38

Every time you use scanf with this format :

scanf("%c",&a); 

it leaves a newline which will be consumed in the next iteration. Th last program that you mentioned have only one "scanf". try to use another scanf. you will get the same problem.

so to avoid white spaces you have to write :

 scanf(" %c",&opr); 

the space before the format string tells scanf to ignore white spaces. Or its better to use

getchar();

It will consume all your newline

The problem is that you are leaving the \n entered after the numbers unconsumed and then is read by the second scanf(). If you check the value in opr you'll see it's '\n'.

The second program do take the \n as the character.
Maybe you simply didn't input \n before inputting other characters.

example ( %c in printf is changed to %d to make it clear)

Try adding fflush(stdin) before scanf.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!