http://cpp.gantep.edu.tr
C++ resources. Faculty of Engineering, University of Gaziantep
MAIN  -  Tutorials  -  Howtos  -  nmPrimer  -  Misc  -  Links  -  Contacts

Complete: ###################- (99%)

Obtaining memory size, and limits, of numerical data types
1.   Obtaining the size of data types
2.   Obtaining lower and upper limits of integers data types

1.   Obtaining the size of data types

The unary operator sizeof() is used to calculate the size in bytes of data types, variables and arrays. The use of this operator is given in the following program.

sizeof.cpp: Outputs the size in bytes of predefined data types using the sizeof() operator
 ``` 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: ``` ```// The use of sizeof() operator to get size of data types #include using namespace std; int main() { cout << "sizeof(char) : " << sizeof(char) << endl; cout << "sizeof(short) : " << sizeof(short) << endl; cout << "sizeof(int) : " << sizeof(int) << endl; cout << "sizeof(long) : " << sizeof(long) << endl; cout << "sizeof(unsigned char) : " << sizeof(unsigned char) << endl; cout << "sizeof(unsigned short) : " << sizeof(unsigned short) << endl; cout << "sizeof(unsigned int) : " << sizeof(unsigned int) << endl; cout << "sizeof(unsigned long) : " << sizeof(unsigned long) << endl; cout << "sizeof(float) : " << sizeof(float) << endl; cout << "sizeof(double) : " << sizeof(double) << endl; cout << "sizeof(long double) : " << sizeof(long double) << endl; return 0; }``` ```sizeof(char) : 1 sizeof(short) : 2 sizeof(int) : 4 sizeof(long) : 8 sizeof(unsigned char) : 1 sizeof(unsigned short) : 2 sizeof(unsigned int) : 4 sizeof(unsigned long) : 8 sizeof(float) : 4 sizeof(double) : 8 sizeof(long double) : 16 ```

The result obtained by this operator is platform dependent. Here some examples for different compilers and operating systems are given below:

 Windows (32 bit) Turbo C Windows (32 bit) Salford FTN95 Windows (32 bit) Dev-C++ Linux (32 bit) GCC Linux (64 bit) GCC ```char : 1 short : 2 int : 2 long : 4 unsigned char : 1 unsigned short : 2 unsigned int : 2 unsigned long : 4 float : 4 double : 8 long double : 10 ``` ```char : 1 short : 2 int : 4 long : 4 unsigned char : 1 unsigned short : 2 unsigned int : 4 unsigned long : 4 float : 4 double : 8 long double : 10 ``` ```char : 1 short : 2 int : 4 long : 4 unsigned char : 1 unsigned short : 2 unsigned int : 4 unsigned long : 4 float : 4 double : 8 long double : 12 ``` ```char : 1 short : 2 int : 4 long : 4 unsigned char : 1 unsigned short : 2 unsigned int : 4 unsigned long : 4 float : 4 double : 8 long double : 12 ``` ```char : 1 short : 2 int : 4 long : 8 unsigned char : 1 unsigned short : 2 unsigned int : 4 unsigned long : 8 float : 4 double : 8 long double : 16 ```

Since sizeof() can also evaluate the size of an array, it can be used to find the number of elements of an array. This is illustrated in the following program.

sizeof2.cpp: Outputs the size in bytes of predefined data types using the sizeof() operator
 ``` 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: ``` ```// The use of sizeof() operator to get size and // number of elements of an array. #include using namespace std; int main() { const int n = 10; double array[n] = { 23.5, 32.8, 33.9, 31.3, 26.9, 21.3, 19.4, 23.7, 26.9, 31.6 }; cout << "sizeof(array) = " << sizeof(array) << endl; cout << "Number of elements = " << sizeof(array)/sizeof(double) << endl; return 0; }``` ```sizeof(array) = 80 Number of elements = 10 ```

2.   Obtaining lower and upper limits of integers data types

The lower and the upper limits of integer data types (char, int, short and long) can be evaluated as follows:
Lower limit = -28*sizeof(dataType)
Upper limit = 28*sizeof(dataType) - 1
For example, for a 4-byte int type:
Lower limit = -28*sizeof(int) = -232 = -2147483648
Upper limit = 28*sizeof(int)-1 = 232-1 = 2147483647
There are some constants defined in <climits> that give these limits for integers:

limits.cpp: Outputs the constants defined in <climits>
 ``` 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: ``` ```// Outputs the constants defined in climits #include #include using namespace std; int main() { cout << "minimum char = " << CHAR_MIN << endl; cout << "maximum char = " << CHAR_MAX << endl; cout << "maximum unsigned char = " << UCHAR_MAX << endl; cout << endl; cout << "minimum short = " << SHRT_MIN << endl; cout << "maximum short = " << SHRT_MAX << endl; cout << "maximum unsigned short = " << USHRT_MAX << endl; cout << endl; cout << "minimum int = " << INT_MIN << endl; cout << "maximum int = " << INT_MAX << endl; cout << "maximum unsigned int = " << UINT_MAX << endl; cout << endl; cout << "minimum long = " << LONG_MIN << endl; cout << "maximum long = " << LONG_MAX << endl; cout << "maximum unsigned long = " << ULONG_MAX << endl; return 0; }``` ```minimum char = -128 maximum char = 127 maximum unsigned char = 255 minimum short = -32768 maximum short = 32767 maximum unsigned short = 65535 minimum int = -2147483648 maximum int = 2147483647 maximum unsigned int = 4294967295 minimum long = -9223372036854775808 maximum long = 9223372036854775807 maximum unsigned long = 18446744073709551615 ```

Notes:
1. The minimum limit for unsigned integers is zero, and is not defined in <climits>.
2. The above output is obtained for gcc 4.3 under 64-bit Linux. If you compile and run the program with another program you might see different results. For example, Dev-C++ under Windows XP (32-bit) gives the following result indicating there is no difference between int and long:
```minimum char           = -128
maximum char           = 127
maximum unsigned char  = 255

minimum short          = -32768
maximum short          = 32767
maximum unsigned short = 65535

minimum int            = -2147483648
maximum int            = 2147483647
maximum unsigned int   = 4294967295

minimum long           = -2147483648
maximum long           = 2147483647
maximum unsigned long  = 4294967295
```