海湾合作委员会并未就转换和数据丢失发出警告

Sae*_*ani 2 c gcc mingw

我在Windows上使用GCC 4.9.2并且发现它没有警告从double转换为int,而visual studio编译器会:

代码:

int main()
{   
    int celsius, fahrenheit, kelvin;
    celsius = 21;
    fahrenheit = celsius * 9 / 5 + 32;
    kelvin = celsius + 273.15; //here it should warn!

    printf("%d C = %d F = %d K", 
        celsius, fahrenheit, kelvin);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译使用:

gcc hello.c -Wall -Wextra -pedantic -std=c99
Run Code Online (Sandbox Code Playgroud)

我使用visual studio编译器编译了相同的代码:

C:\temp>cl hello.c /nologo /W4 /FeC2f.exe
hello.c
hello.c(14): warning C4244: '=': conversion from 'double' to 'int', possible loss of data
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Sha*_*our 7

您需要使用-Wconversion标志,并gcc发出警告:

warning: conversion to 'int' from 'double' may alter its value [-Wconversion]
 kelvin = celsius + 273.15; //here it should warn!
Run Code Online (Sandbox Code Playgroud)

为什么它没有启用,-Wall或者-Wextra在链接的wiki中包含:

隐式转换在C中非常常见.这与前端没有数据流(参见下一个问题)的结果相关,导致难以避免完美工作和有效代码的警告.Wconversion设计用于特定用途(安全审计,将32位代码移植到64位等),程序员愿意接受并解决无效警告.因此,如果未明确请求,则不应启用它.