Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

OVERFLOW IN C Char overflow in c 1. Cyclic nature of unsigned char: Consider following c program: #include<stdio.

h> void main(){ unsigned char c1=260; unsigned char 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:

If number New value If number New value

is X where X is greater than 255 then = X % 256 is Y where Y is less than 0 then = 256 (Y% 256)

2. Cyclic nature of signed char: #include<stdio.h> int main(){ signed char c1=130; signed char 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.

Shortcut formula to find cyclic value: If number is X where X is greater than 127 then p = X % 256 if p <=127 New value = p else New value = p 256 If number is Y where Y is less than -128 then p = Y % 256 If p <= 127 New value = -p else New value = 256 -p

Integer overflow in c Cyclic nature of unsigned int: Range of unsigned int is 0 to 653535. If we will assign a value greater than 653535 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 0 then we have to move in anti clockwise direction.

Short cut formula to find cyclic value: If number New value If number New value is X where X is greater than 65535 then = X % 65536 is Y where Y is less than 0 then = 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 then p = X % 65536 if p <=32767 New value = p else New value = p - 65536 If number is Y where Y is less than -32768 then

p = Y % 65536 If p <= 32767 New value = -p else New value = 65536 -p Note: Same rule is also applicable in case of signed long int and unsigned long int.

Float overflow in c
What will happen if we will go beyond the range of float, double and long double data type? Answer: If we will assign a value which is beyond the maximum value of that data type compiler will assign +INF if number is positive and INF if number is negative. If we will assign a value witch is less than minimum value of that data type then complier will assign a garbage value or zero.

double data type overflow :

For example: What will be output of following c code?


#include<stdio.h> int main(){ float pmax= 3.5e38f; float nmin=-3.3e38f; float min= 1.0e-38f; printf("%f %f %f",pmax,nmin,min); return 0; }

You might also like