#include #include /*************************************************** * Integer data type limits * * C++ provides several built-in integer data types. * The most commonly used signed integer types are * * short * int * long * long long * * Each also has a corresponding unsigned type: * * unsigned short * unsigned int * unsigned long * unsigned long long * * Different integer types may use different numbers * of bits and consequently may represent different * ranges of values. * * * BYTES AND BITS * * The expression * * sizeof( TYPE ) * * gives the number of bytes used to store an object * of the specified type. * * For example, * * sizeof( int ) * * gives the number of bytes used by an 'int'. * * Be careful: a C++ byte is not defined to contain * exactly eight bits on every possible system. * * We can determine the number of bits in a byte using * * std::numeric_limits::digits * * On essentially all computers you are likely to use, * this value is 8. * * * SIGNED AND UNSIGNED TYPES * * A signed integer type can represent negative, * zero, and positive values. * * An unsigned integer type can represent only * non-negative values. Its minimum value is therefore * always zero. * * If a signed and unsigned type use the same number * of bits, the unsigned type can represent larger * positive values because it does not need to * represent negative values. * * * THE EXACT SIZES ARE NOT FIXED * * C++ does not require 'short', 'int', 'long', and * 'long long' to have one particular number of bytes * on every computer. * * It does require their sizes to satisfy * * sizeof( short ) * <= sizeof( int ) * <= sizeof( long ) * <= sizeof( long long ) * * and the standard guarantees minimum ranges for * these types. * * On the Linux computers used for this course, you * will normally see * * short 2 bytes * int 4 bytes * long 8 bytes * long long 8 bytes * * On 64-bit Windows, however, 'long' is normally * only 4 bytes while 'long long' is 8 bytes. * * * std::numeric_limits * * The library allows us to ask C++ about the * properties of a particular data type. * * For example, * * std::numeric_limits::min() * * gives the minimum value that can be represented by * an 'int', while * * std::numeric_limits::max() * * gives the maximum value. * * Similarly, * * std::numeric_limits::max() * * gives the maximum value that can be represented by * an 'unsigned int'. * * This is preferable to writing the numerical limits * directly into our program because the program can * determine the correct limits for the system on * which it is compiled. ***************************************************/ // Function declaration int main(); // Function definition int main() { std::cout << "There are " << std::numeric_limits::digits << " bits in a byte on this system." << std::endl << std::endl; ///////////////////////////// SHORT ////////////////////////////////// std::cout << " >>>> SHORT <<<<" << std::endl << std::endl; std::cout << "A 'short' is " << sizeof( short ) << " bytes." << std::endl; std::cout << "The type 'short' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'unsigned short' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; ////////////////////////////// INT /////////////////////////////////// std::cout << " >>>> INT <<<<" << std::endl << std::endl; std::cout << "An 'int' is " << sizeof( int ) << " bytes." << std::endl; std::cout << "The type 'int' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'unsigned int' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; ////////////////////////////// LONG ////////////////////////////////// std::cout << " >>>> LONG <<<<" << std::endl << std::endl; std::cout << "A 'long' is " << sizeof( long ) << " bytes." << std::endl; std::cout << "The type 'long' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'unsigned long' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; /////////////////////////// LONG LONG ///////////////////////////////// std::cout << " >>>> LONG LONG <<<<" << std::endl << std::endl; std::cout << "A 'long long' is " << sizeof( long long ) << " bytes." << std::endl; std::cout << "The type 'long long' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'unsigned long long' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; ////////////////////// SIZE RELATIONSHIPS ///////////////////////////// std::cout << "The sizes satisfy:" << std::endl; std::cout << " sizeof(short) = " << sizeof( short ) << std::endl; std::cout << " sizeof(int) = " << sizeof( int ) << std::endl; std::cout << " sizeof(long) = " << sizeof( long ) << std::endl; std::cout << " sizeof(long long) = " << sizeof( long long ) << std::endl; return 0; }