GeeksforGeeks » C/C++ Programming Questions

A C Puzzle

(4 posts)

Tags:

  1. karan
    guest
    Posted 1 year ago #

    int data; // Initialize data during run time
    if ( data == -data && data!=0)
           printf ("Miracle!!");
    

    Will miracle ever print ?

  2. Venki
    Moderator
    Posted 1 year ago #

    Yes. When 'data' is on its left extreme of the number scale.

  3. mario
    Member
    Posted 1 year ago #

    @venki you mean to say when data is = -32467 ?

  4. Venki
    Moderator
    Posted 1 year ago #

    @Mario, small correction. It is -32768 on 16-bit platforms and -2147483648 on 32-bit platforms.

    In general the range is -2^(N-1) to 2^(N-1) - 1. The left extreme will have same pattern even when numerically negated. This is due to the fact that 2's complement form of any number which is exactly power of 2 will result in same number. And, we can't have 2^(N-1) on the +ve side of the scale. Ofcourse, zero is special... always.

    In case of floating point numbers we can have -ve zero and +ve zero. The bit pattern of floating point numbers is different.


Reply

You must log in to post.

RSS feed for this topic