简体   繁体   中英

scanf() for Integer OR Character

So let's say I have a while loop that will end when the condition "isDone" is set to true. Inside the while loop, I ask the user to either enter an integer to continue the loop or a character to end it (by setting "isDone" to true).

How would I go about this using only one scanf()?

The suggestion above is good advice: you're probably better off not using scanf() at all. It might be better to use something like fgets() instead, and then parse the string. Or even better, just use getchar() .

But anyway ...

Remember that, in C, a "character" IS an integer. So just scanf("%c", &i) and check if you've got an "integer" ( isdigit() ) or a "character" ( isalpha() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM