Home>Article>Backend Development> What is the input format character of double?

What is the input format character of double?

醉折花枝作酒筹
醉折花枝作酒筹 Original
2021-05-17 14:38:15 23511browse

When using the scanf statement, the input format character of double is "%lf", and "%f" cannot be used; when using the printf statement, you can use "%f". %lf is not defined in printf, but there are many The system will accept it. Therefore, it is recommended to use "%lf" input when using double type to avoid errors.

What is the input format character of double?

The operating environment of this tutorial: Windows 7 system, C 17 version, Dell G3 computer.

(1) The %f specifier of printf can output both float and double types.

According to the "default parameter promotion" rule (in the variable parameter list of a function like printf, this rule applies regardless of whether there is a prototype in the scope) float type will be promoted to double type. So printf() will only see doubles.

(2) scanf must use %f for float type, and %lf for double type. For scanf, the situation is completely different. It accepts pointers, and there is no similar type promotion here.

Storing to float (through pointer) is quite different from storing to double. Therefore, scanf distinguishes between %f and %lf.

(3) In fact, %lf is not defined in printf, but many systems may accept it. To ensure portability, stick to %f.

It is recommended that when using the double type, use %lf input and %f output to avoid errors.

Recommended tutorial: "C#"

The above is the detailed content of What is the input format character of double?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn