【发布时间】:2015-08-21 05:19:19
【问题描述】:
我有以下程序
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int main(void) {
uint16_t o = 100;
uint32_t i1 = 30;
uint32_t i2 = 20;
o = (uint16_t) (o - (i1 - i2)); /*Case A*/
o -= (uint16_t) (i1 - i2); /*Case B*/
(void)o;
return 0;
}
案例 A 编译没有错误。
案例 B 导致以下错误[error: conversion to ‘uint16_t’ from ‘int’ may alter its value [-Werror=conversion]]
我使用的警告选项是:-Werror -Werror=strict-prototypes -pedantic-errors -Wconversion -pedantic -Wall -Wextra -Wno-unused-function
我在 Ubuntu 15.04 64 位上使用 GCC 4.9.2。
为什么我在案例 B 中收到此错误,而在 案例 A 中却没有?
PS: 我用 clang 编译器运行了同样的例子,两种情况都编译得很好。
【问题讨论】:
-
你为什么要首先这样做?
-
不要使用这些选项。
标签: c gcc integer-promotion uint16