C++: difference between 0. and 0.0?

∥☆過路亽.° 提交于 2019-12-06 03:52:10

There is no difference. Both literals are double. From the C++-Grammar:

fractional-constant:
    digit-sequenceopt . digit-sequence
    digit-sequence .

See: Hyperlinked C++ BNF Grammar

No, there is not.

No. You can also write .0 as far as I know.

Just having the . as part of the number identifies it as a floating point type.

This:

 cout << (5 / 2) << endl;
 cout << (5. / 2) << endl;
 cout << (5.0 / 2) << endl;

Prints this:

 2
 2.5
 2.5

You can see that the first line uses integer division (because both values are integers), whereas 5. and 5.0 both get identified as floating point types, and so they trigger "normal division."

the_drow

0 is of type int but can be casted to double and 0.0 is of type double but can be casted to int.
Both casts are implicit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!