c - 程序中的多个 scanf 导致 c 崩溃?

标签 c

#include <stdio.h>

int main()
{       
    printf("how old are you? ");
    int age = 0;
    scanf("%d", age);

    printf("how much does your daily habit cost per day? \n");
    int daily = 0;
    scanf("%d", daily); 

    double thisyear = daily * 365;

    printf("\n");
    printf("this year your habit will cost you: %.2f", thisyear);

    return 0;
}

这是我的学校计划,当我写这篇文章时,我试图让用户达到 1,给出他们的年龄和 2,他们的日常生活费用。但是当我运行这个程序时我的程序会崩溃

最佳答案

<罢工> scanf("%d", 每天);

需要成为

scanf("%d", &daily);

您需要将变量的地址(即指针,这是通过 & 完成的)传递给 scanf以便可以更改变量的值。这同样适用于您的其他提示。改成

scanf("%d", &age);

现在你应该在运行你的程序时得到这个:

% a.out
how old are you? 30
how much does your daily habit cost per day? 
20

this year your habit will cost you: 7300.00

关于c - 程序中的多个 scanf 导致 c 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867567/

相关文章:

c - 在 C 中返回数组?

c - 数组中两个值的除法总是返回 0

c - 如何将参数传递给函数,从 Assembly 到 C

c - Windows Defender 防病毒 API

C memcpy() 段错误

c - 是否有推荐的方法将标题与 C 中的数据输出对齐?

c++ - C++ 宏展开的一个问题

c - 基于C的抽象数据类型列表数组中的memcpy

c - 为什么减去字符实现的行为是特定的?

调用多个 _popen 失败