简体   繁体   English

printf%e科学计数法在C中给出了错误的值

[英]printf %e scientific notation giving wrong value in C

I'm doing a very basic printout of scientific notation. 我正在做一个非常基本的科学计数法打印输出。

But, it appears to me its not working and I can't figure out why? 但是,对我来说似乎不起作用,我不知道为什么吗?

Code: 码:

size_t result = 0;
printf("%e \n",result);

Value it prints is below: 它打印的值如下:

1.278341e-307

What would be right way to do scientific notations? 做科学记数法的正确方法是什么?

If you use a floating-point printf format code, like %e , the matching argument must be a double . 如果使用%e类的浮点printf格式代码,则匹配参数必须为double But you are passing it an integer ( size_t ). 但是,您要传递一个整数( size_t )。

Printf does not know the types of its arguments, so you have to tell it what they are by using the correct format code. Printf不知道其参数的类型,因此您必须使用正确的格式代码来告知它们的含义。 If you lie to it, you will get undefined results. 如果您撒谎,将会得到不确定的结果。

And if you are using gcc or clang, please always use the -Wall command-line option and read the warnings . 如果使用的是gcc或clang,请始终使用-Wall命令行选项并阅读警告 That will save all of us a lot of trouble. 这将为我们所有人省去很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM