I have a bit of C code, which goes exactly like this:
short int fun16(void){
short int a = 2;
short int b = 2;
return a+b;
}
When I try to compile it with GCC, I get the warning:
warning: conversion to 'short int' from 'int' may alter its value [-Wconversion]
return a+b;
^
Though there is no visible conversion. Both operands are short and even the returning value is short as well. So, what's the catch?
To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused.
-Werror= Make the specified warning into an error. The specifier for a warning is appended, for example -Werror=switch turns the warnings controlled by -Wswitch into errors.
By default, Fuchsia C/C++ code compiles with the flag -Wconversion , which prohibits implicit type conversions that may alter a value.
-Werror is a compiler flag that causes all compiler warnings to be treated as errors. Developers who enable -Werror are making a statement: we care about our code base, and we won't accept warnings here.
When you do arithmetic computations, the operands are subject to "the usual arithmetic conversions" (a superset of the "integer promotions" quoted in Acme's answer—he beat me to this but I'll go ahead and post anyway :-) ). These widen short int
to plain int
, so:
a + b
computes the same result as:
((int) a) + ((int) b)
The return
statement must then narrow this int
to a short int
, and this is where gcc produces the warning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With