C++ Basic 3 : Data-types
When you declare a variable in C++ (see the previous tutorial for details) you are creating a 'box' with a name in memory that can store a value. The size of that box depends on what you want to store in it.
This means that you must tell C++ the type of data that you wish to store in the variable, otherwise the box might not be big enough. There are a number of different data-types, suitable for storing different types of data. Here's the main data-types, showing the range of values and the number of bytes required to store them:
Data-types for storing numbers
| Type | Description | Range | Bytes | |
| short int | A small whole number | From: | -32768 | 2 |
| to: | 32767 | |||
| unsigned short int | A small whole number | From: | 0 | 2 |
| to: | 65535 | |||
| int | A whole number | From: | -2147483648 | 4 |
| to: | 2147483647 | |||
| unsigned int | A whole number | From: | 0 | 4 |
| to: | 4294967295 | |||
| float |
A floating point number |
From: | 1.4 x 10-45 | 4 |
| to: | 3.4 x 1038 | |||
| double |
A floating point number |
From: | 2.2 x 10-308 | 8 |
| to: | 1.8 x 10308 | |||
Data-types for other purposes
| Type | Description | Range | Bytes | |
| char |
A single character (e.g. 'A') stored as a byte |
From: | 0 | 1 |
| to: | 255 | |||
| bool | A true or false value | From: | 0 | 1 |
| to: | 1 | |||
Some examples
Take a look at the code snippet below:
int iInteger = 3462345;
short int iShortInt = 22441;
char chLetter = 'A';
The images below show you the size comparison between the different variables that were declared in the code above. Each box represents a byte:



Be careful when mixing data-types
Because there's quite a few different data-types for storing numbers you'll need to be extra careful when working with them. The main reason for this is because of how they're handled.
If you try to add a float to an int, the float will be truncated into an integer. What truncation means is that what is after the decimal place is chopped off. The number is not rounded up!
Thus, 3.876 becomes 3, not 4!
Take a look at this example program:
// Include the iostream library
#include <iostream>
// Indicate that commands are being used from the std namespace
using namespace std;
// Main function (no inputs)
int main(void){
// Declare an integer and store 4127 in it
int iInteger = 4127;
// Declare a float and store 3.675 in it
float fFloat = 3.675;
// Add fFloat to what's already in iInteger
iInteger = iInteger + fFloat;
// Add iInteger to what's already in fFloat
fFloat = fFloat + iInteger;
// Display the contents of each variable
cout << "iInteger = " << iInteger << endl;
cout << "fFloat = " << fFloat << endl;
// Wait for the user to press a key
system("PAUSE");
// Return success
return EXIT_SUCCESS;
} // End the main function
This program creates two variables; iInteger and fFloat. One is an integer and one is a float (I've even named them to make this clear). In pictures, this is what's happening:
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
Error warning! For some weird reason I put 4133 in the box above (fFloat on the second line down). That should be 4130.675! I'll correct this when I find my original layered image files. Sorry. 
Notice that when the result is being stored in an integer (the top row of images) the digits after the decimal point are just removed as opposed to the number being rounded up as you might expect.
Because this could be such a serious problem with accuracy most C++ compilers will warn you if this truncation may take place. To the right is the warning that Dev-C++ provides during compilation of my example program above.
The compiler marks this line to indicate where the error is:
iInteger = iInteger + fFloat;
Using casting to over-ride truncation/conversion warnings
If you are sure that you don't care what is after the decimal point then you can explicitly tell the compiler that you want it to treat the float as an integer. This is called casting (i.e. casting the float as an integer).
It's really easy to do - wrap the variable in brackets and put the name of the data-type to cast the variable as just in front of the brackets.
So, to over-ride the warning for the line above I need to cast fFloat as an integer.
Before casting fFloat:
// Add fFloat to what's already in iInteger
iInteger = iInteger + fFloat;
After casting fFloat as an integer:
// Add fFloat to what's already in iInteger
iInteger = iInteger + int(fFloat);
Note: The floating point number will still be truncated. You just won't be warned about it in the compiler. 
