Fundamental Types

 < Day Day Up > 



C++ comes with several built-in data types ready for immediate use. Each of the character, integer, and floating point types has a corresponding range of values they should represent. I use the word “should” because each compiler implementation can choose to increase the value range of a particular type so long as they meet certain American National Standards Institute (ANSI) requirements as specified in the C++ standard.

Your compiler defines its fundamental data type value ranges in the following header files: climits.h, limits.h, and cfloat.h. The value ranges of each type is largely hardware dependent. For example, a 32-bit processor will have a smaller maximum long integer value than a 64-bit processor, assuming the compiler is keeping pace with the hardware it is running on.

Table 5-1 gives the type value ranges for the Metrowerks CodeWarrior C++ compiler version 5.0.

Table 5-1: Fundamental Types and their Value Ranges

Type

Minimum Value

Maximum Value

bool

false (0)

true (1)

signed char

-128

127

char

-128

127

unsigned char

0

255

wchar_t

0

65,535

signed short int

-32,768

32,767

short int

-32,768

32,767

unsigned short int

0

65,535

signed int

-2,147,483,648

2,147,483,647

int

-2,147,483,648

2,147,483,647

unsigned int

0

4,294,967,295

signed long int

-2,147,483,648

2,147,483,647

long int

-2,147,483,648

2,147,483,647

unsigned long int

0

4,294,967,295

signed long long int

-9,223,372,036,854,775,808

9,223,372,036,854,775,807

long long int

-9,223,372,036,854,775,808

9,223,372,036,854,775,807

unsigned long long int

0

18,446,744,073,709,551,615

float

1.17549e-38

3.40282e+38

double

2022507e-308

1.079769e+308

long double

2022507e-308

1.079769e+308

Note: These ranges are valid for Metrowerks CodeWarrior C++ version 5.0 for Macintosh. Your ranges may look similar or somewhat different for some of the larger types depending on your compiler implementation.

Determining Your Data Type Ranges

There are a couple of ways to determine your data type ranges. The first is by inspecting the climits.h and cfloat.h header files. In them you will find the valid ranges listed for each fundamental type. If you are not used to reading commercial grade header files this is an excellent exercise. Another benefit to this approach is that it lets you get better acquainted with your development environment.

The second way to determine your fundamental type ranges is by calculation. The limits.h file in newer implementations of C++ defines the numeric_limits template class. Example 5.3 gives a short program showing you how to use the numeric_limits class to calculate a type’s range. If you don’t feel comfortable using template classes just yet don’t worry. The inspection method is all you need for now.

The line numbers to the left of example 5.3 are not part of the source code.

Listing 5.3: Using numeric_limits Template Class to Calculate Type Ranges

start example
 1    #include <iostream>  2    #include <limits>  3  4    using namespace std;    5  6    int main(){  7         numeric_limits<int> _i;  8         cout<<"Integer Range:  "<<_i.min()<<"  "<<_i.max()<<endl;  9         return 0; 10    }
end example

Running this short program produces the output shown in Figure 5-5.

click to expand
Figure 5-5: Results of Running Example 5.3

I’d like to take a moment to explain what’s happening here. On line 7 a numeric_limits<int> object named _i (pronounced underscore i) is declared. The numeric_limits<int> type is an example of how to create a new type with a template class. The numeric_limits<> class has several methods a programmer can use to get implementation specific information regarding fundamental data types. Two of these methods are used on line 8. The first is min(), and the second is max(). Calling the min() and max() functions on the _i object results in the output of an integer’s minimum value and its maximum value.

The numeric_limits< > class can be used on all the C++ data types. For example, if you need information regarding the char type, just use char instead of the int type used in example 5.3 when creating the numeric_limits object. Expanding example 5.3 to print out all the data type ranges for your compiler implementation is left as an exercise.

Determining Data Type Size with the sizeof Operator

The size of the data type directly determines the range of values it can effectively store. The bigger the type, the more bits it has available to represent data. If you need to know how large a particular data type is in bytes you can use the sizeof operator. To use the sizeof operator first declare an object of a particular type then use the sizeof operator on the object. Example 5.4 shows a short program that declares an integer object and then uses the sizeof operator to report its size:

Example 5.4: Using the sizeof Operator

start example

click to expand

end example



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net