Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explain the Outcome of C program [duplicate]

I saw following code which prints output "Same" but i am having trouble in understanding this program. Please help me to understand the program.

int  main() 
{ 
   unsigned int x = -1; 
   int y = ~0; 
   if(x == y) 
      printf("same"); 
   else
      printf("not same"); 
   return 0; 
}

How the output "Same" comes? Please help me about what happens here.

like image 988
Destructor Avatar asked Dec 24 '22 23:12

Destructor


1 Answers

Unsigned int x = -1 has the bit flags (32 bits) :

 11111111111111111111111111111111

int 0 has the bit flags (32 bits) :

 00000000000000000000000000000000

~0 is the negate of 0 (bitwise) which is (32 bits) :

 11111111111111111111111111111111

As a side note :

 unsigned int x = -1;  

is equivalent to

 unsigned int x = UINT_MAX. 
like image 199
MichaelCMS Avatar answered Jan 07 '23 07:01

MichaelCMS