【问题标题】:Get wierd Value of my double [closed]获得我双倍的奇怪价值[关闭]
【发布时间】:2017-07-27 14:50:31
【问题描述】:

在我的p 和q 中输入值后,我在scanf 中输入值,然后在printf 中输入值。为什么?

void gyllenSnitten(void);
void Andragradare(void);

double p,q;
double root1, root2;

void main()
{
    gyllenSnitten();
    Andragradare();
}

void gyllenSnitten(void){
    printf("Gyllende snittet är %d..\n",(1+sqrt(5))/2)
}

void Andragradare(void){

   system("cls");

   printf("P-q formeln,\n");
   printf("Choice youre P \n");

   scanf_s("%1f",&p);
   printf("Choice youre Q\n");
   printf("%1f", &q);

   root1 = (-p / 2) + sqrt(((p / 2)*(p / 2)) + q);
   root2 = (-p / 2) - sqrt(((p / 2)*(p / 2)) + q);

   printf("Here is the first X = %d",root1);
   printf("and the other onehere  X = %d",root2);

   _getch();
}

【问题讨论】:

  • %1f 应该是 %lf,%d 应该是 %f。
  • printf和scanf的文档有什么不清楚的地方?
  • 更改但仍然无法正常工作,我在调试中查看步骤并在我的“p”上获得类似 &p 0x00c6a140 {labb3e.exe!double p} {5.356796015279e-315#DEN} double 的值跨度>

标签: c printf scanf


【解决方案1】:

printf() 的 %d 格式说明符需要 int-type 参数,传递任何其他类型都是未定义的行为。

尝试使用%f 打印doubles。

【讨论】:

  • %f 用于打印double?不应该是%lf吗?
  • @J...S 文档很清楚:两者都是有效的(兼容性原因,您不能将 float 作为可变参数传递。
猜你喜欢
  • 2017-04-23
  • 2023-02-22
  • 1970-01-01
  • 2012-07-17
  • 2013-03-30
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多