C | Data Types | Question 1

Predict the output of following program. Assume that the numbers are stored in 2’s complement form.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h> 
int  main() 
   unsigned int x = -1; 
   int y = ~0; 
   if (x == y) 
      printf("same"); 
   else
      printf("not same"); 
   return 0; 
}

chevron_right


(A) same
(B) not same


Answer: (A)

Explanation: -1 and ~0 essentially have same bit pattern, hence x and y must be same. In the comparison, y is promoted to unsigned and compared against x (See this for promotion rules). The result is “same”. However, when interpreted as signed and unsigned their numerical values will differ. x is MAXUNIT and y is -1. Since we have %u for y also, the output will be MAXUNIT and MAXUNIT.


Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.