452 views
0 votes
0 votes

what is the output of the following programming????

and am confusing is unsigned int stores signed integer, and 

what is ‘ ’ this symbol ?? and what happened when assigning ‘~0’ to y????? and 

what will be printed when x,y are printing and how …?

can anyone tell me how it happened ..Thankyou…..!

#include<stdio.h>

int  main()

{

   unsigned int x = -1;

   int y = ~0;

   if (x == y)

      printf("same");

   else

      printf("not same");

printf("\n x is %u, y is %u", x, y);

   return 0;

}

 

1 Answer

0 votes
0 votes
1) When you assign the sign bit to unsigned bit then first converted to 2s complement

-1 => 1| 1 where first 1 is sign and second represent value

Now applying sign bit extension rule so 1111...1 upto the size/capacity of x

2) y=~0 means flip bits and represent in 2s complement i.e -(0+1) = -1

3) x==y then from y=-1 it is converted to unsigned which is 1111.1 upto the size/capacity of y which is same as size taken by x

Hence x and y contains same value

so same is printed

And output in 64 bit compiler is 4294967295 4294967295 here also converting signed to unsigned for y
edited by

Related questions

1 votes
1 votes
1 answer
1
2 votes
2 votes
1 answer
2
atul_21 asked Sep 10, 2017
747 views
2 votes
2 votes
1 answer
3
Abhishek Rawat asked Jul 10, 2017
429 views
✓ main(){ unsigned short int a=-4;printf("%d",a);}Output=65532✓ main(){ unsigned int a=-4;printf("%d",a);}Output=-4Why this happens???
1 votes
1 votes
1 answer
4
Imarati Gupta asked Jan 1, 2017
3,978 views
A function that does the same operation on different data types is to be implemented using a) macros b) overloading c) function templates d) default arguments