C/C++中 使用scanf和printf如何读入输出double型数据。
ringa_lee
ringa_lee 2017-04-17 13:45:23
0
2
1316

C/C++中 使用scanfprintf如何读入输出double型数据。

使用%lf,出现了如图所示的情况

应该scanf使用%lfprintf使用%f么?
为什么?

ringa_lee
ringa_lee

ringa_lee

reply all(2)
黄舟

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;
左手右手慢动作

If you can double, try not to use float. Although it takes up a bit of memory, try not to mix the two

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template