1) The compiler is not "shouting" (a more correct description is "issuing a diagnostic") about implicit conversions because the conversion from a signed
type to unsigned
type is well defined by the standard - it uses what is (mathematically) known as modulo arithmetic. The call of set_data(&a, -10)
does such a conversion of -10
BEFORE calling set_data()
. A value of -10
when converted to unsigned
will result in a value of UINT_MAX - 9
where UINT_MAX
is obtained from <limits.h>
and represents the largest value a unsigned int
can represent. Compilers can often be configured to give warnings about such conversions but, by default, most compilers are configured NOT to. If you want warnings, you'll need to read documentation for your compiler to work out how.
2) All of your calls of printf()
result in undefined behaviour, since %d
informs printf()
that the corresponding argument is of type int
, and you have passed an unsigned
(i.e. a type mismatch). With your particular implementation, it appears that conversion of values of type int
to or from unsigned
does not change the bitwise representation, and the unsigned
value passed to printf()
has the same bitwise representation as an int
with value -10
. That is NOT guaranteed by the standard.
3) As I've said in the previous point, your call of printf()
has undefined behaviour, due to a type mismatch. Change the format specifier to %u
to get well-defined behaviour (and you will then see the value printed will be equal to UINT_MAX-9
. The conversion of an int
with a negative value to unsigned
- which occurs when calling set_data()
in main()
has implementation-defined behaviour since the value of UINT_MAX
is implementation-defined. In other words, the value that results from converting -10
from int
to unsigned
is implementation-defined.
Note, also, that the const
specifier on the second parameter of set_data()
is redundant, as far as the caller is concerned, since that argument is passed by value. Even without the const
, any changes the function makes to data
will only be visible within the body of set_data()
and invisible to the caller.
I'm not going to give links to other SO threads, since my answer is reasonably complete without them. You can search for such links for yourself.