Table of Contents
c cast 연산자 우선순위
c cast 연산자 우선순위는 곱셈보다 높다.
아래 첫번째 계산에서 cast 연산이 먼저 이루어지므로,
overflow 가 발생한다.
#include <stdio.h>
#include <stdint.h>
#define MEGA 1000000
int main() {
uint64_t u = 4000;
u = u * MEGA;
int i = (int) u / MEGA; // overflow
printf("%d\n", i);
int j = u / MEGA; // ok
printf("%d\n", j);
int k = (int) (u / MEGA); // ok
printf("%d\n", k);
}
출력값이 아래와 같다.
-294
4000
4000