私はこのコードを書きました:
#include <stdio.h>
int main(){
printf("Size of short int: %d \n", sizeof(short));
printf("Size of int: %d \n", sizeof(int));
printf("Size of long int: %d \n", sizeof(long));
printf("Size of float: %d \n", sizeof(float));
printf("Size of double: %d \n", sizeof(double));
printf("Size of long double: %d \n", sizeof(long double));
return 0;
}
出力は次のとおりです。
Size of short int: 2
Size of int: 4
Size of long int: 4
Size of float: 4
Size of double: 8
Size of long double: 12
当然のことながら、整数と浮動小数点データ型には違いがありますが、int と同じ量のメモリを long に割り当てるコンパイラの背後にある理由は何ですか? long はより大きな値を処理するように設計されていますが、上記のように行うと役に立ちません (整数の場合)。浮動小数点の long 型では、16 ビットの割り当てが追加されます。
私の質問は、本質的に、その能力を利用しないマシンのインスタンスがある場合、なぜ長く持つのですか?
K&R 電子ブックから:
The intent is that short and long should provide different lengths of integers where practical; int will
normally be the natural size for a particular machine. short is often 16 bits long, and int either 16 or
32 bits. Each compiler is free to choose appropriate sizes for its own hardware, subject only to the the
restriction that shorts and ints are at least 16 bits, longs are at least 32 bits, and short is no longer
than int, which is no longer than long.
マシンのコンパイラが int よりも long に多くのメモリを割り当てることを選択する場合の「経験則」はありますか? およびその逆?基準は何ですか?