Computer Language/C

[C] Integer

정우섭 2019. 12. 1. 19:26

Overflow

  Overflow occurs if the input number is greater than the storage range of type

 
1
2
3
4
5
6
7
8
9
#include <stdio.h>
 
int main()
{
    int n = 2147483638;
 
    for(int i =1; i <= 20; i++)
        printf("%d\n", n++);
}
 

          Result : 2147483638

                     2147483639

                     2147483640

                     2147483641

                     2147483642

                     2147483643

                     2147483644

                     2147483645

                     2147483646

                     2147483647

                    -2147483648

                    -2147483647

                    -2147483646

                    -2147483645

                    -2147483644

                    -2147483643

                    -2147483642

                    -2147483641

                    -2147483640

                    -2147483639

 

 

The storage range of 'int' is (-2147483648~2147483647).

 

While compiling loop in line7 'n' overflows, so the value resets into minimun number.