Programs to show size of built in data types and user defined datatypes, using sizeof operator

Programs to show size of built in data types and user defined datatypes, using sizeof operator

1.
#include <iostream>

class Z{

};

class X{
    int x;
};

class Y{
    int z;
    char name[20];
    X a;
};

main()
{
    std::cout<<"Char "<<sizeof(char)<<std::endl;
    std::cout<<"int "<<sizeof(int)<<std::endl;
    std::cout<<"float "<<sizeof(float)<<std::endl;
    std::cout<<"double "<<sizeof(double)<<std::endl;
    std::cout<<"long "<<sizeof(long)<<std::endl;
    std::cout<<"short "<<sizeof(short)<<std::endl;
    std::cout<<"void "<<sizeof(void)<<std::endl;
    std::cout<<"bool "<<sizeof(bool)<<std::endl;
    std::cout<<"long long "<<sizeof(long long)<<std::endl;


    std::cout<<"Class with one int var Xclass X{int x;};\tsize="<<sizeof(X)<<std::endl;
    std::cout<<"Empty class"<<sizeof(Z)<<std::endl;
    std::cout<<"Class Y class Y {int z;char name[20];X a;};\tsize="<<sizeof(Y)<<std::endl;
}
/*

Output:

Char 1
int 4
float 4
double 8
long 4
short 2
void 1
bool 1
long long 8
Class with one int var Xclass X{int x;};        size=4
Empty class1
Class Y class Y {int z;char name[20];X a;};     size=28

*/

Post a Comment

0 Comments