型のサイズ
環境によって各型のサイズは異なるので、実際に出力させて確認すべき。
【実行結果】
char : 1 short int : 2 int : 4 long int : 8 float : 4 double : 8 long double : 16
【出力プログラム】
#include <iostream> #include <iomanip> using namespace std; int main(){ // ここだけの話、leftは1行目だけにあればよい様子。ちゃんと理由もあるのかもしれない。しかし、今は調べないので気になる人はご自分で調べてください。 cout << setw(12) << left << "char" << setfill(' ') << ": " << sizeof(char) << endl; cout << setw(12) << left << "short int" << setfill(' ') << ": " << sizeof(short int) << endl; cout << setw(12) << left << "int" << setfill(' ') << ": " << sizeof(int) << endl; cout << setw(12) << left << "long int" << setfill(' ') << ": " << sizeof(long int) << endl; cout << setw(12) << left << "float" << setfill(' ') << ": " << sizeof(float) << endl; cout << setw(12) << left << "double" << setfill(' ') << ": " << sizeof(double) << endl; cout << setw(12) << left << "long double" << setfill(' ') << ": " << sizeof(long double) << endl; return 0; }