Variables
In C++, all Variables must be assigned a valid data type. The data types
of C++ are the same as in C:
- bool
- char
- int
- short
- double
- float
- long
- struct
- class
- enum
Declaring vs Defining variables
In C++, there are 2 ways of introducing variables. The first way is called
declaration of a variable. The second is called definition of a variable.
The following code snippet shows the differences:
The following codes show how to declare a variable:
int i; //declaration of i as type int
string s; //declaration of s as type string
After declaring a variable, you must assign a value to it.
There are two ways to assign a value:
int i = 2; //declare variable i and assign value 2 to it Or:
int i; //declare data type;
i = 2; //then assign a value. |