Why isn't “0f” treated as a floating point literal in C++?

后端 未结 6 648
暖寄归人
暖寄归人 2020-12-03 21:19

Why isn\'t 0f treated as a floating point literal in C++?

#include 

using namespace std;

int main(){
  cout << 0f <&l         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-03 21:43

    Because the 0 is an integer constant.

    edit: The error message given by codepad.org (assume g++) may be a little easier to understand. "error: invalid suffix "f" on integer constant". A "0.f" will work because 0. (or 0.0, same thing) is a decimal constant, and asking for a decimal constant to be a float makes more sense than asking for an integer constant to be a float :)

提交回复
热议问题