1,937 views
1 1 vote
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.

Position:
Show:

Related questions

0 0 votes
2 2 answers
1.1k
1.1k views
iarnav asked Dec 27, 2017
1,102 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 0 votes
1 1 answer
811
811 views
Wanted asked Jan 5, 2017
811 views
0 0 votes
3 3 answers
1.0k
1.0k views
Prabhaprince asked Sep 25, 2024
1,007 views
int i =-3; unsigned short u; u=i; printf ("%u",u); printf("%d",u);What is the output and why ?
0 0 votes
0 0 answers
703
703 views
shaz asked Dec 13, 2018
703 views
How is the output still -4? when we are storing it in an unsigned int here shouldn’t it be converted to 2’s complement and then the absolute value of it be the result? i...