为什么printf没有使用科学记数法?

Sim*_*on. 8 c printf pow

我知道这是一个常见的问题.但是我找不到一个可靠的直接答案.

16 ^ 54 = 1.0531229167e+65 (this is the result I want)
Run Code Online (Sandbox Code Playgroud)

当我使用时pow(16,54),我得到:

105312291668557186697918027683670432318895095400549111254310977536.0

代码如下:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void main(){

   double public;
   double a = 16;
   double b = 54;
   public = (pow(a,b));

   printf("%.21f\n", public);
}
Run Code Online (Sandbox Code Playgroud)

执行代码:

gcc main.c -lm

我做错了什么?

das*_*ght 23

我究竟做错了什么?

几件事:

  • 使用%.10e科学计数法格式,printf点后十位数的打印输出,
  • int从你的回来main,
  • 考虑不使用public命名变量,可能需要将程序移植到C++,其中public是关键字.

以下是修复程序的方法:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){

   double p;
   double a = 16;
   double b = 54;
   p = (pow(a,b));

   printf("%.10e\n", p);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

在ideone上演示.

  • `public` 不是 C 中的关键字。:) 尽管如果此代码转移到 C++,它的使用将会出现问题 (2认同)

abe*_*nky 9

你有没有尝试过:

printf("%e\n", public);
Run Code Online (Sandbox Code Playgroud)

%e说明符是科学记数法,如文档中所述