Notes: scanf dead lock problem and rand() in C

本文探讨了在C语言中正确使用Scanf函数的方法,包括如何避免输入死锁及解决措施,并讨论了Rand函数使用时应注意的问题,如种子设置不当会导致随机数序列重复。

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

Scanf has a serious problem if you don't use it correctly. For example:


int x,y;

scanf("%d%d",&x,&y);


If we input a charter instead of a number, system will continue using the last effective input (input buffer) as input and never give you the chance to input. In order to get rid of this deadlock, we should use setbuf() or rewind() to clear the input buffer. 


In the context of the code, we should input two integers with space in the middle of them. Or we should input one integer and enter 'enter', then input another integer. We should not use comma or other symbols between two integers. If we use a comma in the middle, system will only read the first integer for x. Then the integer after comma will combine with the next first input integer. 


Rand() problem is that if we use the same seed each time, the sequence of generated numbers will be the same. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值