J's Study log

[C] Integer 본문

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.

 

 

'Computer Language > C' 카테고리의 다른 글

[C] Pointer  (0) 2020.04.28
[C] Structure Pointer  (0) 2020.02.08
[C] Data Type  (0) 2019.12.01
[C] Format specifier  (0) 2019.12.01
[C] Operator Priority  (0) 2019.12.01
Comments