C/C++中 使用scanf
和printf
如何读入输出double型数据。
使用%lf
,出现了如图所示的情况
应该scanf
使用%lf
,printf
使用%f
么?
为什么?
黄舟2017-04-17 13:47:23
Note that the scanf and printf functions are unusual functions in that neither limits the function's arguments to a fixed number. The scanf function and printf function have variable length parameter lists. When calling a function with a variable-length parameter list, the compiler will arrange for the float parameter to be automatically converted into a double type. As a result, the printf function cannot distinguish between float and double type parameters. Therefore, in the printf function call, %f can represent both float and double parameters.
On the other hand, the scanf function points to a variable through a pointer. %f tells the scanf function to store a float value at the passed address location, and %lf tells the scanf function to store a double value at the passed address location. The difference between float and double is very important here. If the wrong conversion specification is given, the scanf function may store the wrong number of bytes (it is not mentioned that the bit pattern of a float may be different from the bit pattern of a double).
The answer is here
Search before asking
If CPP can be used, iostream will be easier to use
cin>>doubleValue;
cout<< doubleValue;
ringa_lee2017-04-17 13:47:23
If you can double, try not to use float. Although it takes up a bit of memory, try not to mix the two