c语言 scanf("%c");fflush(stdin),c language scanf - fflush(stdin) - doesnt work - Stack Overflow

在C语言中,使用scanf多次读取输入时遇到问题,程序未等待新输入而直接退出。通过在转换说明符前添加空格解决了问题,这与输入流中的换行符有关。然而,尝试使用fflush(stdin)清除输入流并未奏效。示例代码展示了该现象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This question already has answers here:

Closed 4 years ago.

When I use scanf more than one time the program do not wait for another input. Instead it exits

I learned that I could put a blank space before the conversion specifier in the scanf-function - yes that solved the problem and I guess that has to do with the inputstream, that is - if its a newline character in the inputstream the scanf will consume it immediately.

scanf(" %f", &value);

But if its so - why could I not use the fflush(stdin) instead? I have tried but it doesnt work.

#include

int main(void)

{

float value;

char ch;

printf("input value: ");

scanf("%f", &value);

fflush(stdin);

printf("input char: ");

scanf("%c", &ch);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值