2.1 Data types
The kind of data that variables may hold in a programming language is called data types. Data types in C++ can be broadly classified in three categories depicted in figure 1.
a) User-defined data type
b) Built-in data type
c) Derived data type
The use-defined data typeenables the programmer to invent his/her own data types and defines what values it can take on.
Derived data types are built from the basic integer and floating-point data types. The array data type is one example of derived data types. An array can hold several values of the same type, under one variable name.
Built-in data type
.
Integral Type
This can be further classified into
ii. char
intis the basic data type. It is treated as an integer in that cannot hold fractional values.
charis a data type which can hold both the character data or the integer data. For example after making the declaration
char c;
you can make either of the following assignments:
c = ‘A’;
c = 65;
In both cases the decimal value 65 is loaded into the character variable c.
Floating Type
Floating type can further be classified into:
i. float
ii. double
floatmeans floating data-type and represent fractions such as
0.356
0.000001
doublestands for double precision. It is capable of holding a real number ranging from 1.7 x 10 –308 to 1.7 x 10 308 which gives twice as much precision as presented by a float. The precision refers to the number of decimal places that can be represented.
void type
voiddata type has two important purposes:
i. To indicate that the function does not return a value
ii. To declare a generic pointer
For example you may see a function definition such as:
void func(a, b)
This indicates that a function does not return any useful value. Likewise, on the calling side you would declare a func() as:
extern void func();
This informs the compiler that any attempt to use the returned value from func()is a mistake and should be flagged as an error. Foe example, you could invoke func() as follows:
func(x, y);
But you cannot assign the returned value to a variable.
2.2 Declarations
Every variable must be declared before being used. Declaration provides the compiler with about how many bytes should be allocated and how those bytes should be represented. Usually declarations of the same data trype are grouped together. For example
int j, k;
float x, y, z;
The word int and floatare reserved words specifying the integer data type and real data type. There are nine reserved words for data types in C++ as given below:
int char float double short signed void long unsigned
Typical range and size of these data types are given in table 1.
Table 1: Typical range and size of basic data types.
| Type | Range | Byte | Represents | |
| | From | To | | |
| char/short | -128 | 128 | 1 | characters |
| 0 | 225 | 1 | characters | |
| int | -32,768 | 32,768 | whole numbers | |
| unsigned int | 65,535 | 2 | whole numbers | |
| long | -2,147,438,648 | 2,147,438,648 | 4 | whole numbers |
| unsigned long | 0 | 4,294,967,295 | 4 | whole numbers |
| float | 3.4 x 10-38 | 3.4 x 1038 | 4 | fractional numbers |
| double | 1.7 x 10-308 | 1.7 x 10308 | fractional numbers | |
| long double | 3.4 x 10-4932 | 3.4 x 104932 | 10 | fractional numbers |
If you wish to store integer values less than –32,768 or grater that 32,768, you should use declaration
long int.
If you need to store integer values in the range of –32,768 and 32,768, then you can use short int.
Unsigned integers
In a situation where a variable is to hold non-negative values such as counting things, such variables are restricted to non-negative numbers (or unsigned), thereby doubling its positive range. Looking at table 1 we note that signed short int has a range of -32,767 to 32,767 whereas an unsigned short inthas a range from 0 to 65,535.
To declare an integer variables as being non-negative only, use the unsignedqualifier as shown below:
unsigned int k;
unsigned short k;
unsigned long n;
Characters and integers
C++ makes a distinction between numeric and character data. The data type charcan be used to hold either characters or numbers. For example, after you make the declaration
char = c;
You can make either of the following assignments:
c = ‘A’;
c = 65;
Note that character constants are enclosed in single quotes. The quotes tell the compiler to get the numeric code (ASCII code value) of the character.
2.3 Constants
C++ supports three types of character constants namely:
i. Integer constants
ii. Floating-point constants
iii. String constants
Integer constants
Integer constants are values, which are mostly used by programmers and computer users. A computer can also uses octal and hexadecimal constants. Octal constants are written by preceding the octal value with the digit zero. A hexadecimal constant is written by preceding the value with zero and an x or X. Table 2 illustrates integer constants in octal and hexadecimal equivalences.
Table 2: Integer constants
| Decimal | Octal | Hexadecimal |
| 3 | 003 | 0x3 |
| 8 | 010 | 0x8 |
| 15 | 017 | 0xf |
| 16 | 020 | 0x10 |
| 21 | 025 | 0x15 |
| -87 | -0127 | -0x57 |
Floating-point constants
Because an integer data type is inadequate for presenting very large or very small numbers, floating point data types are therefore necessary. Table 3 illustrates valid and invalid floating-point constants.
| Floating point Constants | Comment | Remarks |
| Valid | | |
| .4444444 | Valid | |
| 0.4 | Valid | |
| 3e2 | Valid | |
| 5E-2 | Valid | |
| 3.7e12 | Valid | |
| 35 | Invalid | No decimal point or exponent |
| 3,500.25 | Invalid | Commas are illegal |
| 6E | Invalid | The exponent must be followed by a number |
| 3e2.5 | Invalid | The exponent value must be an integer |
String constants
A sequence of zero or more characters enclosed by double quotes is called a string constant. An example of a string is:
“My name is Juma”
A blank string constant is simply represented by “ “.
The backslash character (\) alters the meaning of the character that follows it. Thus the character ‘n’ when typed after the backslash, i.e. \n, will mean to print a new line. Table 4 gives a list of backslash character strings and the action they produce.
Table 4: Typical range and size of basic data types
| Backslash Character | Meaning |
| \a (alert) | Produce an alert (or a bell) |
| \b (backspace) | Move the cursor back one space |
| \f (form feed) | Move the cursor to the next page |
| \n (new line) | Print a new line |
| \r (carriage return) | Print a carriage return |
| \t (horizontal tab) | Prints a horizontal tab |
| \v (vertical tab) | Prints a vertical tab |
No comments:
Post a Comment