编译器不显示显式转换的警告消息
Compiler not showing warning messages for explicit casts
我最近下载了 MinGw 编译器,但出于某种原因,当我应该进行显式转换时它没有显示警告消息。
例如:
double x=9.97;
int y=x;
编译器似乎没问题。我只是想知道我是否需要安装其他软件包
将 double
分配给 int
时不需要转换。小数部分只是被截断了。如果你想要警告,你可以使用 -Wconversion
或 -Wfloat-conversion
.
为了补充另一个答案,关于隐式转换语义的技术解释可以在 http://en.cppreference.com/w/c/language/conversion:
上找到
A finite value of any real floating type can be implicitly converted to any integer type. Except where covered by boolean conversion above, the rules are:
- The fractional part is discarded (truncated towards zero).
. If the resulting value can be represented by the target type, that value is used
. otherwise, the behavior is undefined
我最近下载了 MinGw 编译器,但出于某种原因,当我应该进行显式转换时它没有显示警告消息。
例如:
double x=9.97;
int y=x;
编译器似乎没问题。我只是想知道我是否需要安装其他软件包
将 double
分配给 int
时不需要转换。小数部分只是被截断了。如果你想要警告,你可以使用 -Wconversion
或 -Wfloat-conversion
.
为了补充另一个答案,关于隐式转换语义的技术解释可以在 http://en.cppreference.com/w/c/language/conversion:
上找到A finite value of any real floating type can be implicitly converted to any integer type. Except where covered by boolean conversion above, the rules are:
- The fractional part is discarded (truncated towards zero).
. If the resulting value can be represented by the target type, that value is used
. otherwise, the behavior is undefined