V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Hansee
V2EX  ›  问与答

if (scanf("%li %li", &x, &y) != 2 ) 请问这个条件应该怎么理解?

  •  
  •   Hansee · 2013-09-01 15:59:58 +08:00 · 3322 次点击
    这是一个创建于 3915 天前的主题,其中的信息可能已经有所发展或是发生改变。
    7 条回复    1970-01-01 08:00:00 +08:00
    windywinter
        1
    windywinter  
       2013-09-01 16:02:39 +08:00
    On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
    Hansee
        2
    Hansee  
    OP
       2013-09-01 16:13:02 +08:00
    @windywinter 还是不太明白。其实是希望检验x,y的输入只为数字,且y不为0的判断。在sf上提问后,有人给出了这样的条件。现在输入非数字可以成功判断到,但是y为0还是无法判断。
    nybux
        3
    nybux  
       2013-09-01 16:28:48 +08:00
    判断是不是读取到了2个变量,!=2没读去到,估计在if里面要有额外的错误处理
    Hansee
        4
    Hansee  
    OP
       2013-09-01 16:37:16 +08:00
    @nybux 并且这两个变量会需要符合x和y声明时的类型么?因为现在输入字幕就会进入这个if语句,数字的话就会跳过。
    rteta
        5
    rteta  
       2013-09-01 16:45:43 +08:00
    新手认为是,成功读入两个符合指定数据类型的数 则执行if后面的语句 C语言从来就没学会过,坐等高手指点
    Hansee
        6
    Hansee  
    OP
       2013-09-01 17:20:24 +08:00
    @rteta 我现在也是这么理解的
    wang2191195
        7
    wang2191195  
       2013-09-01 17:20:31 +08:00
    if (scanf("%li %li", &x, &y) != 2 && y != 0 )
    嘿嘿
    @Hansee
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1542 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.