Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned int comparision in c

int main()
{
    unsigned int c = -1;
    char *s = "Abc";
    char *x = "defhe";
    ((strlen(s)-strlen(x))>c)? printf(s): printf(x);
}

the value of c is 4294967295 and the value of (strlen(s)-strlen(x)) is 4294967294 it should print the x but it is printing the s value.I am not getting why it is like that

like image 619
Chandra Sekar Avatar asked Jan 20 '26 17:01

Chandra Sekar


1 Answers

the value of c is 4294967295 and the value of (strlen(s)-strlen(x)) is 4294967294

It isn't necessarily true that (strlen(s)-strlen(x)) yields 4294967294. It depends on the value of SIZE_MAX on your system.

If SIZE_MAX is 18446744073709551615 (typically on a 64-bit system) then (strlen(s)-strlen(x)) will be 18446744073709551614 which is obviously greater than 4294967295 (assuming UINT_MAX is 4294967295). Hence, you see printf(s); getting printed.

Use a printf() statement to see the values and understand:

printf("%zu %u\n", strlen(s)-strlen(x), c);
like image 100
P.P Avatar answered Jan 22 '26 14:01

P.P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!