C++ examples for Operator:sizeof
Demonstrating the sizeof operator on various data types
#include <iostream> using namespace std; int main() /* w w w .j av a2 s . c o m*/ { char c; // variable of type char short s; // variable of type short int i; // variable of type int long l; // variable of type long float f; // variable of type float double d; // variable of type double long double ld; // variable of type long double int array[ 20 ]; // array of int int *ptr = array; // variable of type int * cout << "sizeof c = " << sizeof c << "\tsizeof(char) = " << sizeof( char ) << "\nsizeof s = " << sizeof s << "\tsizeof(short) = " << sizeof( short ) << "\nsizeof i = " << sizeof i << "\tsizeof(int) = " << sizeof( int ) << "\nsizeof l = " << sizeof l << "\tsizeof(long) = " << sizeof( long ) << "\nsizeof f = " << sizeof f << "\tsizeof(float) = " << sizeof( float ) << "\nsizeof d = " << sizeof d << "\tsizeof(double) = " << sizeof( double ) << "\nsizeof ld = " << sizeof ld << "\tsizeof(long double) = " << sizeof( long double ) << "\nsizeof array = " << sizeof array << "\nsizeof ptr = " << sizeof ptr << endl; }