Home > Back-end >  Type double format character when using the scanf function and % % f lf different results? Is there
Type double format character when using the scanf function and % % f lf different results? Is there

Time:02-27

When I was using the scanf function, of type double character I've always thought and % % f the same effect as lf,

I was one of my test code below:


If use % f, the input value is wrong;

If the scanf function with % lf, the result is right;


Why is this? I see another answer, just type double make use % f,

I use the compiler is DEVC++ 5.11.

CodePudding user response:

Double input is 8 bytes, a float is 4 bytes, output use % f no problem, may be improved type,

CodePudding user response:

Since testing, validation, that shows that people say may be limited to the compiler, or other people say ~ if you have any questions

CodePudding user response:

although are floating point Numbers, % f represents single precision floating point data (float), % lf represents double-precision floating-point data (double), how may be the same, single precision floating point number valid number six, seven parts, double-precision floating-point number effectively guarantee 15, section 16, single precision floating point representation of scope: - 3.40 e ~ + 3.40 e, double-precision floating-point representation of range: 1.79 e+308 ~ + 1.79 e+308

CodePudding user response:

You define a double, the scanf with lf, floating point Numbers is very troublesome, see stack

CodePudding user response:

https://blog.csdn.net/weixin_44997886/article/details/102812153
reference

CodePudding user response:

 # include & lt; stdio.h> 
Int main () {
Float f=1.234567 f;
Double lf=1.234567890123456;
Printf (" % f, % 15 lf \ n ", f, lf);//1.234567, 1.234567890123456
Printf (" % f, % f 15 \ n ", f, lf);//1.234567, 1.234567890123456
Printf (" % % lf, 15 lf \ n ", f, lf);//1.234567, 1.234567890123456

Char [] s="1.234567890123456";
F=0.0 f; Sscanf (s, "% f", & amp; F); Printf (" % f \ n ", f);//1.234568
Lf=0.0; Sscanf (s, "% lf", & amp; Lf); Printf (" % 15 lf \ n ", lf);//1.234567890123456
Lf=0.0 f; Sscanf (s, "% f", & amp; Lf); Printf (" % 15 lf \ n ", lf);//0.000000000000000
F=0.0; Sscanf (s, "% lf", & amp; F); Printf (" % f \ n ", f);//70.175720

return 0;
}
//based on the above results available: printf, lf and % % f can be mixed, the scanf when no,

CodePudding user response:

Double is to use % lf
Float use % f
Why don't tangle,

Think about a double can use % d for?
Your problem now is similar to "type double variables, using the scanf function format character % d, why mistake?"

No why, be in the wrong

CodePudding user response:

% % f is a float single precision, lf is a double double precision, so the calculation results are different
  • Related