What is the difference between

前端 未结 4 626
孤街浪徒
孤街浪徒 2020-12-01 14:08

I saw these two parameters in a C example in a C book but the author didn\'t elaborate what the difference between the two are. I know that %f specifies that a

4条回答
  •  粉色の甜心
    2020-12-01 14:47

    For scanf, %f reads into a float, and %lf reads into a double.

    For printf: In C99 and later, they both are identical, and they print either a float or a double. In C89, %lf caused undefined behaviour although it was a common extension to treat it as %f.

    The reason that one specifier can be used for two different types in printf is because of the default argument promotions; arguments of type float are promoted to double when used to call a function and not matching a parameter in a function prototype. So printf just sees a double in either case.

提交回复
热议问题