Correct format specifier for double in printf

后端 未结 5 1073
遇见更好的自我
遇见更好的自我 2020-11-22 06:01

What is the correct format specifier for double in printf? Is it %f or is it %lf? I believe it\'s %f, but I am not sure.<

5条回答
  •  -上瘾入骨i
    2020-11-22 06:29

    Format %lf is a perfectly correct printf format for double, exactly as you used it. There's nothing wrong with your code.

    Format %lf in printf was not supported in old (pre-C99) versions of C language, which created superficial "inconsistency" between format specifiers for double in printf and scanf. That superficial inconsistency has been fixed in C99.

    You are not required to use %lf with double in printf. You can use %f as well, if you so prefer (%lf and %f are equivalent in printf). But in modern C it makes perfect sense to prefer to use %f with float, %lf with double and %Lf with long double, consistently in both printf and scanf.

提交回复
热议问题