|
以下の結果を見ると、計算結果がオーバーフローするときは自動的にlongになっているようです。
そういう動作は定数でしかおこらないと思っていましたが、この動作は規格通りなんでしょうか?
4−1.暗黙の型変換、を読み直しましたが今回の動作を理解できませんでした。
/*
・LSICでの出力結果
A:20000 + 30000 = 50000
B:20000 + 30000 = 50000
C:20000 + 30000 = 50000
D:20000 + 30000 = 50000
E:20000 + 30000 = -15536
*/
#include <stdio.h>
int main(void)
{
short x = 20000;
short y = 30000;
long ansA, ansB, ansC, ansD, ansE;
ansA = x + y;
ansB = (int)x + (int)y;
ansC = ((int)x + (int)y);
ansD = (long)((int)x + (int)y);
ansE = (long)(int)((int)x + (int)y);
printf("A:%hd + %hd = %ld\n", x, y, ansA);
printf("B:%hd + %hd = %ld\n", x, y, ansB);
printf("C:%hd + %hd = %ld\n", x, y, ansC);
printf("D:%hd + %hd = %ld\n", x, y, ansD);
printf("E:%hd + %hd = %ld\n", x, y, ansE);
return 0;
}
|