tyty
tyty

Reputation: 839

C: confirmation of simple arithmetic output

Can I confirm that the following C code gives undefined result for b:

unsigned a = 0;
int b = a - 1;

Upvotes: 3

Views: 92

Answers (3)

Gouse Shaik
Gouse Shaik

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

Jerry Coffin
Jerry Coffin

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

shadyabhi
shadyabhi

Reputation: 17234

Implicit type conversion takes place when you assigned unsigned to int. So, its not undefined behavior.

Upvotes: 0

Related Questions