Programming and Graphics

(Kiana) #1

2.3 Data types 33


char short int long

must be at least as large as the one preceding it. The same applies to the
floating point types:


float double long double

Each must provide at least as much precision as the one preceding it.


The size of the different data types listed in Table 2.3.1 can be confirmed
by using thesizeofoperator discussed in Section 3.1.


Constants


To fix the value of a variable and thus render the variable a constant, we
include the keywordconst. For example, we may declare


const float temperature;

Constants are variables that, once evaluated, remain fixed and thus cease to be
variables.


Aliases


We can introduce an alias of a declared variable so that we can refer to
it by a different name. For example, we may declare:


float a;
float& aalias = a;

Sinceaaliasandaare truly the same variable, any operation on one amounts
to the same operation on the other. In C++, an alias is better known as a
reference.


Defined data types


C++ allows us to duplicate a data type into something that is either more
familiar or more convenient. For example, ifyearis a non-negative integer, we
may declare:


unsigned int year;

Since the year is positive, we have exercised theunsignedoption.


We can duplicate the cumbersome “unsigned int”into“hronos” mean-
ing year in Greek, by stating:

Free download pdf