V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
codechaser
V2EX  ›  C

为什么这个输入字母会一直输出一大串 0?

  •  
  •   codechaser · 2018-10-26 14:19:11 +08:00 · 3096 次点击
    这是一个创建于 1980 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #include <stdio.h>
    
    int main(void) {
        int x = 0;
        while (1){
            x = scanf("%d",&x);
            printf("%d",x);
        }
        return 0;
    }
    
    第 1 条附言  ·  2018-10-26 19:24:20 +08:00
    ```c
    #include <stdio.h>

    int main() {
    int x = 0;

    while (1){
    x = scanf("%d",&x);
    setbuf(stdin,NULL);
    printf("%d",x);
    }

    return 0;
    }
    ```没有一直输出 0 了
    18 条回复    2018-10-26 20:11:53 +08:00
    wevsty
        1
    wevsty  
       2018-10-26 14:24:07 +08:00
    scanf 的返回值并不是输入的内容。
    messyidea
        2
    messyidea  
       2018-10-26 14:24:21 +08:00
    输入字母不应该是 %c 么
    Ginray
        3
    Ginray  
       2018-10-26 14:26:33 +08:00
    一楼正解
    yuikns
        4
    yuikns  
       2018-10-26 14:28:10 +08:00
    https://linux.die.net/man/3/scanf

    These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
    xxgirl2
        5
    xxgirl2  
       2018-10-26 14:45:37 +08:00
    zuoyouTU
        6
    zuoyouTU  
       2018-10-26 16:56:43 +08:00
    稍补充下,等号表达式最后执行
    a516585610
        7
    a516585610  
       2018-10-26 18:33:09 +08:00 via Android
    %d 输入字母会失败,scanf 返回 0 输出当然也是 0
    webdisk
        8
    webdisk  
       2018-10-26 18:35:36 +08:00 via Android
    while (1) 后面是不是少个空格
    aopod
        9
    aopod  
       2018-10-26 18:45:14 +08:00   ❤️ 1
    针对一直输出的问题,或许试试清除缓冲区?
    codechaser
        10
    codechaser  
    OP
       2018-10-26 19:12:26 +08:00
    @wevsty 这个我知道啊,输入字母 scanf 返回 0,打印 0 能理解,但是为什么一直在打印 0 啊?求教
    xiaopc
        11
    xiaopc  
       2018-10-26 19:14:40 +08:00 via Android
    @codechaser 因为你 while (1) 了
    codechaser
        12
    codechaser  
    OP
       2018-10-26 19:16:55 +08:00
    @webdisk 这跟空格有啥关系啊😓
    codechaser
        13
    codechaser  
    OP
       2018-10-26 19:18:09 +08:00
    @xiaopc 但是输入数字就能多次输入
    codechaser
        14
    codechaser  
    OP
       2018-10-26 19:22:53 +08:00
    @aopod 谢谢老哥!我在`x=scanf("%d",&x)`后面加了句`setbuf(stdin,NULL);`就没有一直输出了!感谢!能讲解下原因吗
    xiaopc
        15
    xiaopc  
       2018-10-26 19:25:50 +08:00 via Android   ❤️ 1
    @codechaser 对对,是缓存区的问题。
    scanf 没从 stdin 读到对应数据不会清除缓冲区。
    codechaser
        16
    codechaser  
    OP
       2018-10-26 19:36:45 +08:00
    @xiaopc 是的,scanf 在这里会跳过非数字,把字母一直留在缓冲区,所以每次读入的都是之前输入的字母,也就一直打印 0 了
    jimchen9999
        17
    jimchen9999  
       2018-10-26 19:38:59 +08:00
    the issue lies with the buffer in C
    webdisk
        18
    webdisk  
       2018-10-26 20:11:53 +08:00
    @codechaser 没关系,但是不符合编码规则我就难受
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1007 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:27 · PVG 03:27 · LAX 12:27 · JFK 15:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.