ch3-sizeof operator applied to built-in types

Chapter_3     Exercise_3-5     Bool YourPets     Exercise_3-6







sizeof.cpp     TCP1, p. 143-144 (Specifiers)         download


#include <iostream>
using std::cout;
using std::endl;

int main()
{
char c;
unsigned char cu;
int i;
unsigned int iu;
short int is;
short iis; // Same as short int
unsigned short int isu;
unsigned short iisu;
long int il;
long iil; // Same as long int
unsigned long int ilu;
unsigned long iilu;
long long int ill;
unsigned long long int illu;
float f;
double d;
long double ld;

cout << "sizeof(char) = " << sizeof(char) << " = " << sizeof(c) << endl;
cout << "sizeof(unsigned char) = " << sizeof(unsigned char)
<< " = " << sizeof(cu) << endl;
cout << "sizeof(int) = " << sizeof(int) << " = " << sizeof(i) << endl;
cout << "sizeof(unsigned int) = " << sizeof(unsigned int)
<< " = " << sizeof(iu) << endl;
cout << "sizeof(short) = " << sizeof(short) << " = " << sizeof(is) << endl;
cout << "sizeof(unsigned short) = " << sizeof(unsigned short)
<< " = " << sizeof(isu) << endl;
cout << "sizeof(long) = " << sizeof(long) << " = " << sizeof(il) << endl;
cout << "sizeof(unsigned long) = " << sizeof(unsigned long)
<< " = " << sizeof(ilu) << endl;
cout << "sizeof(long long) = " << sizeof(long long)
<< " = " << sizeof(ill) << endl;
cout << "sizeof(unsigned long long) = " << sizeof(unsigned long long)
<< " = " << sizeof(illu) << endl;
cout << "sizeof(float) = " << sizeof(float) << " = " << sizeof(f) << endl;
cout << "sizeof(double) = " << sizeof(double) << " = " << sizeof(d) << endl;
cout << "sizeof(long double) = " << sizeof(long double)
<< " = " << sizeof(ld) << endl;

return 0;
}
/*
g++ sizeof.cpp -o sizeof
./sizeof
sizeof(char) = 1 = 1
sizeof(unsigned char) = 1 = 1
sizeof(int) = 4 = 4
sizeof(unsigned int) = 4 = 4
sizeof(short) = 2 = 2
sizeof(unsigned short) = 2 = 2
sizeof(long) = 8 = 8
sizeof(unsigned long) = 8 = 8
sizeof(long long) = 8 = 8
sizeof(unsigned long long) = 8 = 8
sizeof(float) = 4 = 4
sizeof(double) = 8 = 8
sizeof(long double) = 16 = 16
*/





Note:  See also ch2-sizeof on the blog Kernighan_and_Ritchie, Chapter_2, Sec. 2.2.









Chapter_3     Exercise_3-5     Bool BACK_TO_TOP YourPets     Exercise_3-6



Comments

Popular posts from this blog

Contents