【问题标题】:Why is scanf not storing user input?为什么 scanf 不存储用户输入?
【发布时间】:2018-01-27 04:01:14
【问题描述】:
float a;
printf("Enter a floating point number:\n");
scanf("%f", &a);
printf("%f", &a);

我试图提示用户输入一个数字,该数字将存储为浮点数,但是当我运行代码时,打印出来的所有内容都是 0.0000。谁能告诉我为什么,我必须改变什么才能在此处打印输入的数字?

【问题讨论】:

  • 因为你没有在printf() 中使用&a——只是a
  • 打印时为什么要打印&a 只打印a。如果要打印地址,请使用%p 而不是%f

标签: c printing floating-point scanf


【解决方案1】:

您正在打印a 的地址,因此您不会得到正确答案。应该是printf("%f", a);

【讨论】:

    【解决方案2】:

    &-operator 仅用于scanf statments 存储在地址。

    试试printf("%f", a);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 2016-09-26
      相关资源
      最近更新 更多