Simple Data Types
Data needs to be stored in order for us to be able to access it for future use like mathematical equations. In order to store data in C++ we must first know and declare the type of data we will be using. For example, if we don't use the correct data type in the right place, we could end up with incorrect results or an error.
There are a handful of data types we will be using in C++ to represent the data we need. But first we must go over the different ways that we can declare a variable. The format for declaring a variable in C++ is
<data type> <variable name>;
<data type> <variable name> = value; // Initialization
Integer
The integer can store a whole number only. The integer can store four bytes. If it is assigned a decimal number the decimal gets truncated, not rounded.
Float and Double
The float data type will hold a decimal number. The float can also hold four bytes. If we wanted to hold a larger decimal number, we could also use the double which can hold eight bytes.
Char
A char is a single character. All characters are stored based on their ASCII value. Every keyboard key has an ASCII value. So when the computer does any type of quality checking it is actually comparing the ASCII value to the ASCII value. Capital letters and lowercase all have their own unique ASCII value. Therefore, they would not be the same.
String
A string is a series of characters put together. Usually used for names, sentences, text, etc.
Boolean
This is a one bit data type that can only store true or false.
int myVar;
float mySalary;
char index = 'A';
string firstName = "Ahmed";
bool isValid = TRUE;
C++ 11 now also allows variables to be declared based on the data type of an expression. For example, if we have Y = X * 5
, we can declare Y to be the same data type as the result of that expression. So if that expression is a whole number, the data type of Y will most likely be an integer.
Another example is to say, Y = X * 3.5
. This statement decltype is declaring Y to be the same data type as the output of the expression which will result in a decimal value. Double is the default data type for any data that will be converted to a decimal. So Y will be declared as a double.
Declaration of variables
It's important to name your variables something meaningful so you remember the data being stored in it and it doesn't get confusing when they're all named random names like x, y, z.
When you declare a variable, depending on the data type of that variable the compiler will go into memory finding another space to hold that particular data type and allocate that for your new variable. If it is an integer, it will get four bytes. If it is bool, only one. Then, it will give it a temporary name of whatever you've named it in your program. So if you have an int to hold your age, the compiler will find four bytes altogether in memory and allocate them for your program. And give them the name "age."
The important part of declaring variables is to initialize the values before you begin because if that memory was old or previously used by another program it may contain some data. The compiler doesn't clear out the data when it allocates it to the program. It just renames it and whatever was there is still there. So always initialize your data for numbers to zero and strings to the empty string so you start with fresh memory storage. The program then keeps a pointer to that memory. So it knows where memory for its program is. Then when its needed it just searches for the matching named memory. If you change the value stored in age the computer goes to the memory block, finds the data named age and overwrites what's in there with the new values.