1,315 views
2 votes
2 votes

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

#include<stdio.h>

int  main()

{

   unsigned int x = -1;

   int y = ~0;

   if (x == y)

      printf("same");

   else

      printf("not same");

   return 0;

}

1.SAME

2.NOT SAME

1 Answer

Best answer
3 votes
3 votes
#include <stdio.h>
int  main() {
  unsigned int x = -1; // bit pattern = all ones
  int y = ~0; // bit pattern = all ones
  if (x == y) // y is promoted to unsigned int 
        // ( as common type) for comparison
    printf("same"); // gets printed
  else
    printf("not same");
  return 0;
}
selected by

Related questions

0 votes
0 votes
2 answers
1
Shashank Kumar Mishr asked May 18, 2017
447 views
What is the output for following code and how integer value can be assigned to a character value?#include <stdio.h>int main(){ char a = 30, b = 40, c = 10; char d =...
1 votes
1 votes
1 answer
2
Shashank Kumar Mishr asked May 17, 2017
711 views
predict the correct options#include <stdio.h>int main(){ if (sizeof(int) -1) printf("Yes"); else printf("No"); return 0;}1.yes2.no3.compiler error...
1 votes
1 votes
1 answer
3
Shashank Kumar Mishr asked May 17, 2017
2,607 views
Assume that the size of char is 1 byte and negatives are stored in 2's complement form#include<stdio.h>int main(){ char c = 125; c = c+10; printf("%d", c); re...
0 votes
0 votes
1 answer
4
Debargha Mitra Roy asked 2 days ago
39 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...