Reputation: 839
Can I confirm that the following C code gives undefined result for b:
unsigned a = 0;
int b = a - 1;
Upvotes: 3
Views: 92
Reputation: 338
It always giving the values
a=0,b=-1
only if you want to check it out in the online c compiler
Upvotes: 0
Reputation: 490018
(§6.3.1.3/3): "...either the result is implementation-defined or an implementation-defined signal is raised."
On typical twos-complement hardware, I'd expect the result to be -1.
Note that the result is implementation defined, not undefined.
Upvotes: 6
Reputation: 17234
Implicit type conversion takes place when you assigned unsigned to int. So, its not undefined behavior.
Upvotes: 0