while函数输入小结

1.知识点1:
scanf函数的返回值可以简单理解为为输入变量的个数:
Both scanf and wscanf return the number of fields successfully converted
and assigned; the return value does not include fields that were read but 
not assigned. A return value of 0 indicates that no fields were assigned. 
The return value is EOF for an error or if the end-of-file character or the 
end-of-string character is nocountered in the first attempt to read a character.
2.知识点2:
逗号表达式取最右边的算数值作为返回值
3.知识点3:
EOF的值其实就是-1,当scanf读取内容发生错误或者读到文件结尾的时候就会返回EOF
/****************************************
输入直到文件末尾
****************************************/
while ((scanf("%d%m",&n,&m) != EOF))
{
//    循环从输入流读取m和n 直到遇到EOF为止,常见于ACM中
}
while (~scanf("%d%d",&m,&n))
{
    //相当于while (scanf("%d",&n) != EOF)
}
/****************************************
输入全为0时跳出循环
****************************************/
while (scanf("%d%d",&n,&m)&&(m|n))
{
    //不断读入新数据,当m,n均为0时跳出循环
}
while (scanf("%d %d", &n, &m), n+m)
{
    //不断读入新数据,当m,n均为0时跳出循环
}
while ((scanf("%d%d",&n,&m) != EOF),n+m)
{
    //有了前面的分析,这个代码就好理解了,神组合啊
}
/****************************************
输入两个整形数,其他类型跳出循环
****************************************/
while (scanf("%d%d", &a, &b)==2)
{
    //不断读入新数据,当输入的不是两个整形数字时,跳出循环
}
while (scanf("%d%d",&n,&m))
{
    //当输入不是两个整形数时,跳出循环
}
/***************************************/
有新的内容会持续更新,有错误的话感谢大家指出

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值