Sunday 15 June 2014

C++ program to find the size of basic data types

C++ program to find the size of basic data types using sizeof() and without using sizeof() and using class.


C++ program to print size of basic data types using sizeof()

#include<iostream>
using namespace std;
int main()
{
    cout<<"size of char        = "<<sizeof(char);
    cout<<"\nsize of int         = "<<sizeof(int);
    cout<<"\nsize of float       = "<<sizeof(float);
    cout<<"\nsize of long        = "<<sizeof(long);
    cout<<"\nsize of double      = "<<sizeof(double);
    cout<<"\nsize of long double = "<<sizeof(long double);
    return 0;
}


C++ program to print size of basic data types without using sizeof()

#include<iostream>
using namespace std;
int main()
{
    char a;
    int b;
    float c;
    long d;
    double e;
    long double f;
    cout<<"size of char        = "<<(char *)(&a+1)-(char *)(&a);
    cout<<"\nsize of int         = "<<(char *)(&b+1)-(char *)(&b);
    cout<<"\nsize of float       = "<<(char *)(&c+1)-(char *)(&c);
    cout<<"\nsize of long        = "<<(char *)(&d+1)-(char *)(&d);
    cout<<"\nsize of double      = "<<(char *)(&e+1)-(char *)(&e);
    cout<<"\nsize of long double = "<<(char *)(&f+1)-(char *)(&f);
    return 0;
}



C++ program to print size of basic data types without using sizeof()

#include<iostream>
using namespace std;
int main()
{
    cout<<"size of char        = "<<(int)(1+(char *)0X0);
    cout<<"\nsize of int         = "<<(int)(1+(int *)0X0);
    cout<<"\nsize of float       = "<<(int)(1+(float *)0X0);
    cout<<"\nsize of long        = "<<(int)(1+(long *)0X0);
    cout<<"\nsize of double      = "<<(int)(1+(double *)0X0);
    cout<<"\nsize of long double = "<<(int)(1+(long double *)0X0);
    return 0;
}



C++ program to print size of basic data types without using class

#include<iostream>
using namespace std;
class size
{
public:
    void printsize()
    {
        cout<<"size of char        = "<<(int)(1+(char *)0X0);
        cout<<"\nsize of int         = "<<(int)(1+(int *)0X0);
        cout<<"\nsize of float       = "<<(int)(1+(float *)0X0);
        cout<<"\nsize of long        = "<<(int)(1+(long *)0X0);
        cout<<"\nsize of double      = "<<(int)(1+(double *)0X0);
        cout<<"\nsize of long double = "<<(int)(1+(long double *)0X0);
    }
};
int main()
{
    size datatypes;
    datatypes.printsize();
    return 0;
}

No comments:

Post a Comment