GeeksforGeeks » Interview Questions

Adobe Written Test Question

(3 posts)
  • Started 4 months ago by Shubhra
  • Latest reply from Abhijeet
  1. Shubhra
    guest
    Posted 4 months ago #

    main()
    {
                  unsigned int y = -12;
                  int x = -2;
                  if (x>y)
                       printf ("x is greater\n");
                  if (x<y)
                       printf("y is greater\n");
    }
    

    Predict the output of the code snippet.

    Answer is - x is greater.

    I executed this on Linux Terminal. Can anyone please explain why x is greater even though it is -2 and y is 12?

  2. Ashish
    guest
    Posted 4 months ago #

    In first Case, unsigned int y = 12;
    The number would be stored in binary form as '0000000000001100 '

    int x = -2;
    Since it is negative, it would store the 2's complement of '2'
    The number would be stored in binary form as '1111111111111110'

    And on comparison, it would be 'x' which is greater than 'y'

  3. Abhijeet
    guest
    Posted 4 months ago #

    @Shubra

    The code you posted says y = -12. I am guessing it should be y = 12
    In that case, explanation by Ashish is correct.


Reply

You must log in to post.

RSS feed for this topic