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

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

  • radha gogia
    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
    anksingla
    thanks radha..
    will it work same for the minimum value...
  • anksingla
    anksingla
    thanks radha..
    will it work same for the minimum value...
  • radha gogia
    radha gogia
    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
    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.

You are reading an archived discussion.

Related Posts

Not found C compiler for widows 64 bit win 8.1. Anyone knows from where can i get it???
Quote: There are no female innovators in the medical device industry. That’s what industry professionals told us when we recently asked for nominations of women researchers, engineers, and designers driving...
As researchers from across the globe are coming up with alternatives for newer sources of green energy, the world is waiting in anticipation for the research to move from 'lab...
Joining few other players, Airtel has jumped into the stores with its brand new music app. Wynk Music by Airtel is the latest audio streaming and downloading app for Android...
The future of computing is here! Internet giant Google and a team from UC Santa Barbara is working on super-fast quantum computing chips. In an official blog post, Hartmut Neven,...