Member • Sep 2, 2014
-
anksinglawhen 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..
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a
member of our community. Consider creating an
account or login.
Replies
-
Member • 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 clearAre you sure? This action cannot be undone. -
Member • Sep 3, 2014
thanks radha..
will it work same for the minimum value...Are you sure? This action cannot be undone. -
Member • Sep 3, 2014
thanks radha..
will it work same for the minimum value...Are you sure? This action cannot be undone. -
Member • Sep 3, 2014
radha gogiathis 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
yes u can continue ur loop starting from minimum or from maximum,it always goes in a chainanksinglathanks radha..
will it work same for the minimum value...Are you sure? This action cannot be undone. -
Member • 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.Are you sure? This action cannot be undone.