サイズの計算にsizeofを使用する

10097 ワード

#include <stdio.h>

#include <iostream>



using namespace std;



int main()

{

   

    //  sizeof             

    cout<<"sizeof('$')="<<sizeof('$')<<endl;

    cout<<"sizeof(1)="<<sizeof(1)<<endl;

    cout<<"sizeof(1.5)="<<sizeof(1.5)<<endl;

    cout<<"sizeof(\"Good!\")="<<sizeof("Good!")<<endl;



    // sizeof             

    int i=100;

    char c='A';

    float x=3.1416; 

    double p=0.1;

    cout<<"sizeof(i)="<<sizeof(i)<<endl;

    cout<<"sizeof(c)="<<sizeof(c)<<endl;

    cout<<"sizeof(x)="<<sizeof(x)<<endl;

    cout<<"sizeof(p)="<<sizeof(p)<<endl;



    // sizeof           

    cout<<"sizeof(x+1.732)="<<sizeof(x+1.732)<<endl;



    //  sizeof           

    cout<<"sizeof(char)="<<sizeof(char)<<endl;

    cout<<"sizeof(int)="<<sizeof(int)<<endl;

    cout<<"sizeof(float)="<<sizeof(float)<<endl;

    cout<<"sizeof(double)="<<sizeof(double)<<endl;



    // sizeof          

    char str[]="This is a test.";

    int a[10];    

    double xy[10];

    cout<<"sizeof(str)="<<sizeof(str)<<endl;

    cout<<"sizeof(a)="<<sizeof(a)<<endl;

    cout<<"sizeof(xy)="<<sizeof(xy)<<endl;



    // sizeof           

    struct st {

        short num;

        float math_grade;

        float Chinese_grade;

        float sum_grade;

    };

    st student1;

    cout<<"sizeof(st)="<<sizeof(st)<<endl;

    cout<<"sizeof(student1)="<<sizeof(student1)<<endl;

    system("pause");

}

出力結果:
sizeof('$')=1

sizeof(1)=4

sizeof(1.5)=8

sizeof("Good!")=6

sizeof(i)=4

sizeof(c)=1

sizeof(x)=4

sizeof(p)=8

sizeof(x+1.732)=8

sizeof(char)=1

sizeof(int)=4

sizeof(float)=4

sizeof(double)=8

sizeof(str)=16

sizeof(a)=40

sizeof(xy)=80

sizeof(st)=16

sizeof(student1)=16

       . . .