The question is, is there a difference between these two statements in your code? Explain. If there is a difference, make sure to provide an example.
I was thinking there is no difference, but if the variable y is unsigned, there is a big difference?
-1 is an expression of type
int, with the value negative one.
0xFFFFFFFF is a constant of type
long int, or
unsigned long int, depending on the implementation. Its value is
4294967295 (232-1). Note that it never has a negative value.
y is of a signed integer type, the first line assigns the value
y is of an unsigned integer type, the value
-1 is converted to
y's type, yielding the maximum value of its type.
4294967295 is within the range of
y's type, then the second line assigns the value
If it isn't, then if
y is of an unsigned type, the value
4294967295 is reduced modulo the maximum value of the type plus one. For example, if
y is of a 16-bit unsigned type, the value assigned is
y is of a signed type that's not big enough to hold
4294967295, then the implicit conversion yields an implementation-defined result. Most commonly, the high-order bits are discarded, but that's not required.
For example, if
y is of a 32-bit signed integer type, then
y=0xFFFFFFFF will most likely assign the value
Finally, converting an out-of-range value to a signed integer type can in principle raise an implementation-defined signal (starting with C99), but I don't know of any implementations that do this.
I've glossed over the possibility of padding bits. I've also ignored the possibility that
y is of a floating-point or complex type.
This answer could have been a lot shorter if you had told us how
y is declared, and what the range of
y's type is on the implementation you're using.
User contributions licensed under CC BY-SA 3.0