c - 使用 scanf 后程序崩溃

标签 c scanf

这是我编写的一个程序,当您给定飞机起飞时间并飞行一秒钟时,它会计算飞机着陆的时间。但是,当我运行程序并在控制台中插入第一个数字时,它立即崩溃。

#include <stdio.h>

int main (void) {
    int m, s;
    int u;
    long flight, landing;

    printf("insert time of taking a plane:\n time in hours: "); 
    scanf("%d, &u");
    printf(" minutes: ");
    scanf("%d", &m);
    printf(" seconds: ");
    scanf("%d", &s);
    printf("Duration of a flight (in seconds): ");
    scanf("%ld", &flight);
    pristanek = u * 3600 + m * 60 + s + let;
    u = landing / 3600;
    m = (landing % 3600) % 60;
    s = landing % 60;
    printf("Time of landing; %d:%02d:%02d\n", u, m, s);
    return 0;
}

最佳答案

这是因为 scanf 的正确语法是:

scanf("%d", &u); // notice the " is after the "d"

您实际上在其他 scanf 中得到了正确的结果。

关于c - 使用 scanf 后程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35183013/

相关文章:

c 中的字符和字符串与 strpbrk

c - 从字符串中删除空格和特殊字符

c - 如何在C中将字符组合在一起作为字符串

c - 有没有办法使用 fscanf 读取以下空白?

c - scanf 传递指针地址(地址变量)的行为

c - 如何在ubuntu上编译doom?

c++ - 扩展宏进行调试?

c - 为什么 fprintf()/fscanf()/printf() 在我的 Dec-to-Binary 程序中显示错误的输出,而 printf() 和 sscanf() 工作正常?

c - 如何检查命令行参数是否为整数

c - 当用户输入 `scanf` DOT 时停止 "."