问题
Consider:
int32_t f() {
return 0x80000000;
}
Why doesn't that cause a compiler warning (at least on GCC)? 0x80000000 is out of the range of int32_t
(INT32_MAX
is 0x7fffffff
). I believe this should cause an implicit cast - is that correct?
Further consder:
if (f() == 0x80000000)
foo();
The above causes no warning on GCC. However
int32 ret = f();
if (ret == 0x80000000)
baz();
Causes "warning: comparison between signed and unsigned integer expressions". I believe this is because 0x80000000 has type unsigned int
due to being out of int
's range. Is that correct?
Assuming none of my assumptions are wrong, why doesn't the first comparison cause a warning?
回答1:
The relevant warning switch appears to be -Wconversion, which is not activated by -Wextra.
回答2:
I believe this is because 0x80000000 has type unsigned int due to being out of int's range. Is that correct?
Yes, since it's a hexadecimal integer literal, it's type would be the narrowest of these types capable of representing the value of the literal:
int
unsigned int
long
unsigned long
long long
unsigned long long
来源:https://stackoverflow.com/questions/32656460/why-doesnt-returning-0x80000000-from-a-function-that-returns-int32-t-cause-a-wa