There are several data types for C++, but these are the frequently used ones:
| Data Type | Size | Description |
|---|---|---|
| integer (int) | 2 or 4 bytes | Stores whole numbers (integers) |
| float | 4 bytes | Stores floating point numbers, but only sufficient for storing 6-7 decimal digits |
| double | 8 bytes | Stores floating point numbers, with the ability to store until 15 decimal digits. |
| boolean (bool) | 1 byte | Stores only two kinds of values: True or False. |
| character (char) | 1 byte | Stores a single character. The character can be a letter, number, or ASCII value. |
| auto | adjust automatically according to the data type | Automatically detects and assigns data type to the variable |
The general rules for naming variables are:
int, float, etc.Creating variables also known as declaring variables is an action of making a variable to store data.
Syntax: data_type variable_name;
data_type is the type of data that you want to store in the variable.
You can review C++'s data type in the section above.
variable_name is the name that you want to give to your variable.
Example:
#include <iostream> int main() { int myNum; return 0; }
We can also declare multiple variables at once.
Syntax: data_type variable_name1, variable_name2, variable_name3;
Example:
#include <iostream> int main() { int myNum1, myNum2, myNum3; return 0; }
Assigning value means storing a value in a variable. You can assign a value when declaring a variable.
Syntax: data_type variable_name = value;
Example:
#include <iostream> int main() { int myNum = 10; return 0; }
We can also assign a value after declaring a variable.
Syntax: variable_name = value;
Example:
#include <iostream> int main() { int myNum; myNum = 10; return 0; }
To change a variable value, we just need to assign a new value to an existing variable. The new assigned value will overwrite the previous value.
Example:
#include <iostream> int main() { int myNum = 10; //myNum is 10 myNum = 20; //myNum is now 20 return 0; }
We can also copy the value of a variable to another variable.
Syntax: variable_name1 = variable_name2;
Example:
#include <iostream> int main() { int myNum1 = 10; int myNum2; myNum2 = myNum1; //myNum2 value is now 10; return 0; }
There is also a variable type called constant.
Constant is used when you do not want the value of the variable to change.
We just need to add the const keyword before the data type in the variable.
Syntax: const data_type variable_name = value;
Example:
#include <iostream> int main() { const int myNum1 = 10; return 0; }
Casting, also known as type conversion is an action of converting a value of one data type to another data type. For example, the conversion of int to float or vice versa. There are two types of conversion in C++:
This conversion occurs automatically when we assign a value to a variable.
Example:
#include <iostream> int main() { int myNum = 9.8; std::cout << myNum; //the output is 9 return 0; }
This conversion is done manually by placing the parentheses symbol in front of the value. So, the data type of the value will only be changed when we put the parentheses symbol.
Example:
#include <iostream> int main() { int myNum1 = 10; float myNum2 = (float) myNum1; std::cout << myNum2; //myNum2 will show as 10.0000 return 0; }
Scope is the area in which a variable can be used or can be accessed. For C++ variables, there are 2 types of scopes, local and global. Local variable exists and can only be accessed in a particular function, while global variable can be accessed by all functions in the entire program. However, the global variable takes up much more memory than the local variable. So, it is advisable to avoid using too many global variables.
Example:
#include <iostream> int phi = 3.14; //global variable int sum (int a, int b){ int result; //local variable inside the sum function } int main() { int myNum1; //local variable inside the main function return 0; }