1,173 views
1 votes
1 votes
int main()
{
unsigned int a = 1000;
int b = -1;
if (a>b) printf("A is BIG! %d\n", a-b);
else printf("a is SMALL! %d\n", a-b);
return 0;
}
the answer to this question is "a is small,1001 "

so I found explaination to this ques and came to some conclusions ,but can someone verify is it correct or not?

1.Here when we compare b(signed int) and a(unsigned int),b is type casted to unsigned and its value is taken as MAX-1,where MAX is the largest unsigned number possible ,so control goes to else condition

2.now a-b=1000-(MAX-1) ,=(-MAX+1001) which is negative and as it has to be treated as unsigned integer its value is MAX+ (-MAX+1001)=1001

3.The presence of "%d " in printf has no effect ,the result will be unsigned always

Please log in or register to answer this question.

Related questions

0 votes
0 votes
2 answers
1
iarnav asked Dec 27, 2017
697 views
How can we convert a signed number to it's equivalent unsigned number?Let's say I have signed number as -2 now what would be it's equivalent unsigned representation. Than...
0 votes
0 votes
1 answer
2
Wanted asked Jan 5, 2017
380 views
0 votes
0 votes
0 answers
3