秋灯烬 发表于 2022-2-20 16:45:59

新人求助,关于c语言的简单问题

本帖最后由 秋灯烬 于 2022-2-20 16:45 编辑

我是刚学的新人,写了一个代码,但是遇到了问题。
#include<stdio.h>
int main()
{
      float floating_point;

      printf("Enter a floating-point value:______\b\b\b\b\b\b ");
      scanf_s("%f",&floating_point);
      printf("\nfixed-point notation: %f \n", floating_point);
      printf("exponential notation: %e\n", floating_point);
      printf("p notation: %a\n", floating_point);
      printf("%.2f", floating_point);

      return 0;
}


随便定义了一个变量后,输入一个数值然后输出各种格式看看,就我粗浅的学识,认为double要比float范围大一些,不考虑什么字节大小之类的情况。

把float---->变成double应该没什么事

但是当我变了以后

Enter a floating-point value: 64.25

fixed-point notation: 64.250000
exponential notation: 6.425000e+01
p notation: 0x1.0100000000000p+6
64.25

也变成了

Enter a floating-point value: 64.25

fixed-point notation: -92559604856675321571320088795986877361484249085968211295338496.000000
exponential notation: -9.255960e+61
p notation: -0x1.ccccc42808000p+205
-92559604856675321571320088795986877361484249085968211295338496.00
就很疑惑,有大佬知道为什么吗

大马强 发表于 2022-2-20 16:56:53

因为double数据格式化输入要用%lf
Enter a floating-point value: 64.25

fixed-point notation: 64.250000
exponential notation: 6.425000e+001
p notation: 0x1.010000p+6
64.25
--------------------------------
Process exited after 31.32 seconds with return value 0
请按任意键继续. . .

#include<stdio.h>
int main()
{
      double floating_point;

      printf("Enter a floating-point value:______\b\b\b\b\b\b ");
      scanf_s("%lf",&floating_point);                           // 要用%lf
      printf("\nfixed-point notation: %f \n", floating_point);
      printf("exponential notation: %e\n", floating_point);
      printf("p notation: %a\n", floating_point);
      printf("%.2f", floating_point);

      return 0;
}

ckblt 发表于 2022-2-20 16:57:19

本帖最后由 ckblt 于 2022-2-20 17:25 编辑

把 scanf_s("%f",&floating_point);
改成 scanf_s("%lf",&floating_point);

秋灯烬 发表于 2022-2-20 17:21:58

ckblt 发表于 2022-2-20 16:57
把 scanf_s("%f",&floating_point);
改成 scanf_s("%lf",&floating_point);



感谢大佬解答

秋灯烬 发表于 2022-2-20 17:22:30

大马强 发表于 2022-2-20 16:56
因为double数据格式化输入要用%lf

感谢大佬解答
页: [1]
查看完整版本: 新人求助,关于c语言的简单问题