繁体   English   中英

当我在 C 代码中包含 scanf() 时,它不起作用

[英]When I include scanf() in my C code it doesn't work

这是代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d",&age);
    printf("You are %d years old!",age);

    return 0;
}

当我运行代码时,我在输出中得到了这个:

[Running] cd "c:\Users\kathh\Documents\Code\C\" && gcc test.c -o test && "c:\Users\kathh\Documents\Code\C\"test

当我强制停止它时,我会在输出中得到这个:

[Done] exited with code=1 in 6.412 seconds

我该怎么办?

尝试添加fflush(stdout); 紧接在调用printf每一行之后。

退出代码 1 表示程序退出不成功。 如果您甚至没有让 printf 输出“输入您的年龄”,那么您就没有正确运行程序,如果您是初学者,请使用 IDE。 另请注意,您应该使用 gets() 函数获取用户输入,然后转换为所需的数据类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM