When value of a variable crosses the range of its datat type it turns into negative why??

anksingla

anksingla

@anksingla-ttYJbX Oct 25, 2024
when we take a value of a variable which is greater than the range of its data type then it turns into a negative value. Can anyone tell why & how this happens..

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • radha gogia

    radha gogia

    @radha-BTDzli Sep 3, 2014

    this because the compiler automatically makes a turn-around ,i.e after it stops on a particular maximum limit of positive range,it again begins from the lowermost point.i.e
    for(i=1;;i++)
    {
    printf("%d",i);
    }
    so actually this is an infinte loop as value of int ranges from -32,768 to +32,767 ,so after it reaches the maximum,again it carries on with minimum value,hope u are clear
  • anksingla

    anksingla

    @anksingla-ttYJbX Sep 3, 2014

    thanks radha..
    will it work same for the minimum value...
  • anksingla

    anksingla

    @anksingla-ttYJbX Sep 3, 2014

    thanks radha..
    will it work same for the minimum value...
  • radha gogia

    radha gogia

    @radha-BTDzli Sep 3, 2014

    radha gogia
    this because the compiler automatically makes a turn-around ,i.e after it stops on a particular maximum limit of positive range,it again begins from the lowermost point.i.e
    for(i=1;;i++)
    {
    printf("%d",i);
    }
    so actually this is an infinte loop as value of int ranges from -32,768 to +32,767 ,so after it reaches the maximum,again it carries on with minimum value,hope u are clear
    anksingla
    thanks radha..
    will it work same for the minimum value...
    yes u can continue ur loop starting from minimum or from maximum,it always goes in a chain
  • pratap singh, upendra

    pratap singh, upendra

    @pratap-singh-6xlmve Sep 15, 2014

    Basically the decimal numbers present in the source code are internally converted to their binary equivalents(since a computer is a digital machine and can understand 1s and 0s only) before they are processed.

    During the processing, the binary sequences are incremented or decremented depending upon the logic implemented and if there occurs an overflow operation, this situation is dealt as per the binary laws. These laws may result in certain binary sequences whose decimal equivalents are negative numbers.

    When the program displays the output, it displays the decimal equivalent of the changed binary sequence(that is representing a negative number). In other words we get a negative number as output.

    For more information on this topic, try to learn binary addition and subtraction in the presence of overflow. The same concepts applies here.