In C for some data types, when we assign a value beyond range of that data type then it won't give any compiler error but assign a number according to some cyclic order. This is known as cyclic nature of data type.
only
char
int
long int
supports cyclic nature.
For example lets see how it happens in char both signed and unsigned.
Range of unsigned char is 0 to 255
0 1 2 3 4 ...... .... .... 255
----------------------------> (clock wise, i.e after 255 it will be 0, 1 and so on)
<--------------------------- (anti clock wire i.e after 0 it will be 255 , 254 and so on)
So if we assign the a value greater than 255 then value of variable will be changed to a value if we will move clockwise direction
If number is less than 0 then move in anti clockwise direction.
You can use this formula to validate:
If number is X and its greater than 255 then
New value = X % 256
If number is Y and its less than 0 then
New value = 256 – (Y% 256)
For sign char its range is -128 to 127
If number is X and its greater than 127 then
p = X % 256
if p <=127
New value = p
else
New value = p – 256
If number is Y and its less than -128 then
p = Y % 256
If p <= 127
New value = -p
else
New value = 256 -p