Why is printf not using scientific notation?

蓝咒 提交于 2019-12-03 07:49:43

What am I doing wrong?

Several things:

  • Use %.10e format for scientific notation with printf for a printout with ten digits after the dot,
  • Return an int from your main,
  • Consider not using public to name a variable, on the chance that your program would need to be ported to C++, where public is a keyword.

Here is how you can fix your program:

#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;
}

Demo on ideone.

Have you tried:

printf("%e\n", public);

The %e specifier is for scientific notation, as described in the documentation

If you need scientific notation you need to use the %e format specifier:

printf("%e\n", public);
        ^^   

Also, public is a keyword in C++ and so it would be a good idea to avoid that and any other keywords in case this code needs to be portable.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!