#include //cout object defined #include //EXIT_SUCCESS/EXIT_FAILURE defined #include //min/max integral values defined #include //min/max floating-point values defined #include //setw(), setiosflags(), resetiosflags() using namespace std; /* * description: This program prints the size of the primitive data types. * Variables are defined and the sizes of those variables are printed. * The size of selected constants are printed (e.g. a constant character, * an integer, a floating-point number, an escape sequence, and a string * literal). All sizes are obtained using the sizeof() operator. * * The minimum and maximum values that can be stored in variables * defined using the primitive data types are printed. These values * are obtained from the limits.h and float.h STDC Library header files. * * notes: By default, cout prints values right-aligned. To change * the alignment, the setiosflags() manipulator is used. The * resetiosflags() manipulator is used to set the I/O flags back to * their default settings. * * Field width can be controlled using the setw() manipulator. * By default, output values are as wide as they need to be. If the * value doesn't fill the field, then padding is supplied. Values * larger than the field are not truncated. * * @creator g.d.thurman * @version 2007.09.15 * * @quote 640,000 bytes of memory ought to be enough for anybody. * --Bill Gates in 1981 */ int main(int, char**) { // define some local (automatic) variables... char c = CHAR_MAX; // In this program, it is not necessary to short s = SHRT_MIN; // to initialize these variables; however, int i = INT_MAX; // in general, it is good idea to initialize long l = LONG_MIN; // local variables when you can (otherwise, float f = FLT_MIN; // their initial values are indeterminate). double d = DBL_MIN; long double ld = LDBL_MAX; // Many compilers will issue a message if you define variables // and never use them (using a variable name as an operand to // the sizeof() operator does not count as using the variable). // Although your goal should be for a "clean-compile" (i.e. one // without warnings), some warnings messages can be ignored. int x = 25; // field width for headings... int y = 4; // field width for sizeof() results... cout << setw(x) << "sizeof(type) = #bytes" << setw(y) << " " << setw(x) << "sizeof(variable) = #bytes" << endl << "=====================" "=========" << "=========================" << endl; cout << setw(x) << resetiosflags(ios::left) << "sizeof(char) = " << setw(y) << setiosflags(ios::left) << sizeof(char) << setw(x) << resetiosflags(ios::left) << "sizeof(c) = " << setw(y) << setiosflags(ios::left) << sizeof(c) << endl << setw(x) << resetiosflags(ios::left) << "sizeof(short) = " << setw(y) << setiosflags(ios::left) << sizeof(short) << setw(x) << resetiosflags(ios::left) << "sizeof(s) = " << setw(y) << setiosflags(ios::left) << sizeof(s) << endl << setw(x) << resetiosflags(ios::left) << "sizeof(int) = " << setw(y) << setiosflags(ios::left) << sizeof(int) << setw(x) << resetiosflags(ios::left) << "sizeof(i) = " << setw(y) << setiosflags(ios::left) << sizeof(i) << endl << setw(x) << resetiosflags(ios::left) << "sizeof(long) = " << setw(y) << setiosflags(ios::left) << sizeof(long) << setw(x) << resetiosflags(ios::left) << "sizeof(l) = " << setw(y) << setiosflags(ios::left) << sizeof(l) << endl << setw(x) << resetiosflags(ios::left) << "sizeof(float) = " << setw(y) << setiosflags(ios::left) << sizeof(float) << setw(x) << resetiosflags(ios::left) << "sizeof(f) = " << setw(y) << setiosflags(ios::left) << sizeof(f) << endl << setw(x) << resetiosflags(ios::left) << "sizeof(double) = " << setw(y) << setiosflags(ios::left) << sizeof(double) << setw(x) << resetiosflags(ios::left) << "sizeof(d) = " << setw(y) << setiosflags(ios::left) << sizeof(d) << endl << setw(x) << resetiosflags(ios::left) << "sizeof(long double) = " << setw(y) << setiosflags(ios::left) << sizeof(long double) << setw(x) << resetiosflags(ios::left) << "sizeof(ld) = " << setw(y) << setiosflags(ios::left) << sizeof(ld) << endl << endl; cout << "sizeof() selected constants" << endl << "===========================" << endl; cout << "sizeof('A') = " << sizeof('A') << endl << "sizeof(100) = " << sizeof(100) << endl << "sizeof(100200210L) = " << sizeof(100200210L) << endl << "sizeof(3.14) = " << sizeof(3.14) << endl << "sizeof('\\n') = " << sizeof('\n') << endl << "sizeof(\"hello, world\") = " << sizeof("hello, world") << endl << endl; x = 12, y = 12; cout << "minimum and maximum manifest constants from " << endl << "======================================================" << endl; cout << setw(x) << resetiosflags(ios::left) << "CHAR_MIN = " << setw(y) << setiosflags(ios::left) << CHAR_MIN << resetiosflags(ios::left) << setw(x) << "CHAR_MAX = " << setw(y) << setiosflags(ios::left) << CHAR_MAX << resetiosflags(ios::left) << setw(x) << "UCHAR_MAX = " << setw(y) << setiosflags(ios::left) << UCHAR_MAX << resetiosflags(ios::left) << endl; cout << setw(x) << "SHRT_MIN = " << setw(y) << setiosflags(ios::left) << SHRT_MIN << resetiosflags(ios::left) << setw(x) << "SHRT_MAX = " << setw(y) << setiosflags(ios::left) << SHRT_MAX << resetiosflags(ios::left) << setw(x) << "USHRT_MAX = " << setw(y) << setiosflags(ios::left) << USHRT_MAX << resetiosflags(ios::left) << endl; cout << setw(x) << "INT_MIN = " << setw(y) << setiosflags(ios::left) << INT_MIN << resetiosflags(ios::left) << setw(x) << "INT_MAX = " << setw(y) << setiosflags(ios::left) << INT_MAX << resetiosflags(ios::left) << setw(x) << "UINT_MAX = " << setw(y) << setiosflags(ios::left) << UINT_MAX << resetiosflags(ios::left) << endl; cout << setw(x) << "LONG_MIN = " << setw(y) << setiosflags(ios::left) << LONG_MIN << resetiosflags(ios::left) << setw(x) << "LONG_MAX = " << setw(y) << setiosflags(ios::left) << LONG_MAX << resetiosflags(ios::left) << setw(x) << "ULONG_MAX = " << setw(y) << setiosflags(ios::left) << ULONG_MAX << resetiosflags(ios::left) << endl << endl; y = 18; cout << "minimum and maximum manifest constants from " << endl << "=====================================================" << endl; cout << setw(x) << "FLT_MIN = " << setw(y) << setiosflags(ios::left) << FLT_MIN << resetiosflags(ios::left) << setw(x) << "FLT_MAX = " << setw(y) << setiosflags(ios::left) << FLT_MAX << resetiosflags(ios::left) << endl; cout << setw(x) << "DBL_MIN = " << setw(y) << resetiosflags(ios::left) << setiosflags(ios::left) << DBL_MIN << resetiosflags(ios::left) << setw(x) << "DBL_MAX = " << setw(y) << setiosflags(ios::left) << DBL_MAX << resetiosflags(ios::left) << endl; cout << setw(x) << "LDBL_MIN = " << setw(y) << setiosflags(ios::left) << LDBL_MIN << resetiosflags(ios::left) << setw(x) << "LDBL_MAX = " << setw(y) << setiosflags(ios::left) << LDBL_MAX << resetiosflags(ios::left) << endl << endl; return EXIT_SUCCESS; } /*** sizeof(type) = #bytes sizeof(variable) = #bytes ======================================================= sizeof(char) = 1 sizeof(c) = 1 sizeof(short) = 2 sizeof(s) = 2 sizeof(int) = 4 sizeof(i) = 4 sizeof(long) = 4 sizeof(l) = 4 sizeof(float) = 4 sizeof(f) = 4 sizeof(double) = 8 sizeof(d) = 8 sizeof(long double) = 12 sizeof(ld) = 12 sizeof() selected constants =========================== sizeof('A') = 1 sizeof(100) = 4 sizeof(100200210L) = 4 sizeof(3.14) = 8 sizeof('\n') = 1 sizeof("hello, world") = 13 minimum and maximum manifest constants from ====================================================== CHAR_MIN = -128 CHAR_MAX = 127 UCHAR_MAX = 255 SHRT_MIN = -32768 SHRT_MAX = 32767 USHRT_MAX = 65535 INT_MIN = -2147483648 INT_MAX = 2147483647 UINT_MAX = 4294967295 LONG_MIN = -2147483648 LONG_MAX = 2147483647 ULONG_MAX = 4294967295 minimum and maximum manifest constants from ===================================================== FLT_MIN = 1.17549e-38 FLT_MAX = 3.40282e+38 DBL_MIN = 2.22507e-308 DBL_MAX = 1.79769e+308 LDBL_MIN = 2.22507e-308 LDBL_MAX = 1.79769e+308 ***/