overflow in c
TRANSCRIPT
-
7/30/2019 Overflow in c
1/6
OVERFLOW IN C
Char overflow in c
1. Cyclic nature of unsigned char:
Consider following c program:#includevoidmain(){
unsignedchar c1=260;unsignedchar c2=-6;printf("%d %d",c1,c2);
}Output: 4 250 (why?)This situation is known as overflow of unsigned char.
Range of unsigned char is 0 to 255. If we will assign
a value greater than 255 then value of variable will be
changed to a value if we will move clockwise direction as
shown in the figure according to number. If number is less
than 0 then we have to move in anti clockwise direction.
Short cut formula to find cyclic value:
-
7/30/2019 Overflow in c
2/6
If number is X where X is greater than 255 thenNew value = X % 256
If number is Y where Y is less than 0 then
New value = 256 (Y% 256)2. Cyclic nature of signed char:#includeintmain(){
signedchar c1=130;signedchar c2=-130;printf("%d %d",c1,c2);return 0;
}Output: -126 126 (why?)This situation is known as overflow of signed char.
Range of unsigned char is -128 to 127. If we will
assign a value greater than 127 then value of variable will be
changed to a value if we will move clockwise direction as
shown in the figure according to number. If we will assign a
number which is less than -128 then we have to move in anti
clockwise direction.
http://1.bp.blogspot.com/_uIwyaTjqYYw/TU5xv79WRZI/AAAAAAAABUY/yaL12XWecJc/s1600/uc.jpeg -
7/30/2019 Overflow in c
3/6
Shortcut formula to find cyclic value:
If number is X where X is greater than 127 then
p = X % 256
if p
-
7/30/2019 Overflow in c
4/6
Short cut formula to find cyclic value:
If number is X where X is greater than 65535 thenNew value = X % 65536If number is Y where Y is less than 0 then
New value = 65536 (Y% 65536)4. Cyclic nature of signed int:
Range of unsigned int is -32768 to 32767. If we will
assign a value greater than 32767 then value of variable will
be changed to a value if we will move clockwise direction as
shown in the figure according to number. If that number is
less than -32768 then we have to move in anti clockwise
direction.
Short cut formula to find cyclic value:
If number is X where X is greater than 32767 thenp = X % 65536if p
-
7/30/2019 Overflow in c
5/6
p = Y % 65536
If p
-
7/30/2019 Overflow in c
6/6
double data type overflow :
For example: What will be output of following c
code?#includeint main(){
float pmax= 3.5e38f;float nmin=-3.3e38f;float min= 1.0e-38f;
printf("%f %f %f",pmax,nmin,min);return 0;
}
http://2.bp.blogspot.com/_uIwyaTjqYYw/TU5z9uJ7ahI/AAAAAAAABUo/4zSG4Itn8Gg/s1600/doublec.jpeg