C++ Data Types

Introduction
What is a data type?

They are means to identify the type of data and associated operations of handling it.

It is a specification to the compiler by the programmer of the type of data to be stored(accepted) in a given variable. When we wish to store data in a C++ program, such as a whole number or a character, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as the range of values that can be stored and the operations that can be performed on the variables of that type.

C++ provides the following standard built-in data types: int, char, float, double, void bool and wchar_t. It also enables us to create our own user-defined data types using enumerations and classes.

For each of these data types the range of values and the operations that can be performed on variables of that data type are determined by the compiler. Each compiler should provide the same operations for a particular data type but the range of values may vary between different compilers.

Boolean Type
The Boolean type can have the value true or false. For example:

bool isEven = false; bool keyFound = true;

If a Boolean value is converted to an integer value true becomes 1 and false becomes 0.

If an integer value is converted to a Boolean value 0 becomes false and non-zero becomes true.

Character Type
The character type is used to store characters - typically ASCII characters but not always. For example:

char menuSelection = 'q'; char userInput = '3';

Note how a character is enclosed within single quotes. We can also assign numeric values to variables of character type:

char chNumber = 26;

You can declare signed and unsigned characters, where signed characters can have positive and negative values, and unsigned characters can only contain positive values.

signed char myChar = 100; signed char newChar = -43; unsigned char yourChar = 200;

Note that if we use a plain char, neither signed nor unsigned:

char dataValue = 27;

it may differ between compilers as to whether it behaves as a signed or unsigned character type. On some compilers it may accept positive and negative values, on others it may only accept positive values. Refer to your compiler documentation to see which applies.

A char is guaranteed to be at least 8 bits in size. C++ also provides the data type wchar_t, a wide character type typically used for large character sets.

An array of characters can be used to contain a C-style string in C++. For example:

char aString[] = "This is a C-style string";

Note that C++ also provides a string class that has advantages over the use of character arrays.

Integer Types
The integer type is used for storing whole numbers. We can use signed, unsigned or plain integer values as follows:

signed int index = 41984; signed int temperature = -33; unsigned int count = 0; int height = 100; int balance = -67;

Like characters, signed integers can hold positive or negative values, and unsigned integers can hold only positive values. However, plain integer can always hold positive or negative values, they're always signed.

You can declare signed and unsigned integer values in a shortened form, without the int keyword:

signed index = 41984; unsigned count = 0;

Integer values come in three sizes, plain int, short int and long int.

int normal = 1000; short int smallValue = 100; long int bigValue = 10000;

The range of values for these types will be defined by your compiler. Typically a plain int can hold a greater range than a short int, a long int can hold a greater range than a plain int, although this may not always be true. What we can be sure of is that plain int will be at least as big as short int and may be greater, and long int will be at least as big as plain int and may be greater. A short integer is guaranteed to be at least 16 bits and a long integer at least 32 bits.

You can declare short and long integer values in a shortened form, without the int keyword:

short smallValue = 100; long bigValue = 10000;

You can have long and short signed and unsigned integers, for example:

unsigned long bigPositiveValue = 12345; signed short smallSignedValue= -7;

Floating-Point Types
Floating point types can contain decimal numbers, for example 1.23, -.087. There are three sizes, float (single-precision), double (double-precision) and long double (extended-precision). Some examples:

float celsius = 37.623; double fahrenheit = 98.415; long double accountBalance = 1897.23;

The range of values that can be stored in each of these is defined by your compiler. Typically double will hold a greater range than float and long double will hold a greater range than double but this may not always be true. However, we can be sure that double will be at least as great as float and may be greater, and long double will be at least as great as double and may be greater.

Derived Data Types:
These are the data types which are derived from the fundamental data types. It is further divide into two categories i)Built-In and ii)User-defined, which are discussed below as seperate topics.

Arrays:
Arrays refer to a list of finite number of same data types. The data in the array can be accessed by an index number ranging from 0 to n(where n is the number of data element it can store). Ex- if arr[3] is an array of int(egers) then the different values in the array can be accessed as shown below. arr[0], arr[1],arr[2] when we declare an array such as the one sown above then by arr[3] we mean that we want three elements in the array and hence while accessing arr[2] is the last element.

//Program to illustrate arrays #include        void main(void) {        int arra[4];//it will store three integer elements cout<<"enter 3 numbers:"; cin>>arra[0]>>arr[1]>>arra[2];//this statement is the same as using 3 cin's

cout<  int main {    int n = 10; int *p;//pointer declaration p = &n; cout<        void main(void) {        int var; int &refvar=var;//here a reference variable to var is declared remember var was previously declared

var=10;//var is given the value 10 cout<<var<<endl; refvar=100;//reference variable of var is changed

cout<<var;//but var also gets changed

}

Class:
A class is a collection of variables and function under one reference name. it is the way of separating and storing similar data together. Member functions are often the means of accessing, modifying and operating the data members (i.e. variables). It is one of the most important features of C++ since OOP is usually implemented through the use of classes.

Structure:
In C++ structure and class same except for some very minor differences.

Union:
A union is a memory location shared by two or more different variables, generally of different data types. Giving more details here would only confuse you; I’ll leave it for future articles.

Enumerations:
It can be used to assign names to integer constants.

//Program to illustrate #include        void main(void) {        enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator

int var; var=POOR;//this makes programs more understandable cout<<var<<endl; var=GOOD; cout<<var<<endl; var=EXCELLENT; cout<<var;

}

Data Types Modifiers
signed unsigned short long

Int, char, float, double data types can be preceded with these modifiers to alter the meaning of the base type to fit various situations properly. Every data type has a limit of the larges and smallest value that it can store known as the range. An integer (usually 4 bytes long) can store any value ranging from -2147483648 to 2147483647. Data Type modifiers usually alter the upper and lower limit of a data type. Unsigned modifier makes a variable only to store positive values. Ex- if a data type has a range from –a to a then unsigned variable of that type has a range from 0 to 2a. Preceding any data type with signed is optional because every data type is signed by default. Short integers are 2 bytes long and long integers are 8 bytes long.