To create a variable, specify the type and assign it a value. You can also declare a variable without assigning the value, and assign the value later.
stores integers (whole numbers), without decimals, such as 123
or -123
.
// Defining a variable with value
int my_number = 15;
// Defining a variable
int my_number2;
// Assigning the value
my_number2 = 15;
stores floating point numbers, with decimals, such as 19.9
or -19.9
.
float myFloatNum = 5.1
float myFloatNum2;
myFloatNum2 = 5.1
stores double floating point numbers, with large values after decimals, such as 19.995165
or -19.95165
.
double myDoubleNum = 5.1512
double myDoubleNum2;
myDoubleNum2 = 5.1512
stores a single character in the variable with single quotes, such as A
or c
.
char myChar = 'A'
char myChar2;
myChar2 = 'c'
const
is used to define a variable that cannot be changed after defined.
const int myNum = 15; //This variable cannot be changed after this.
We cannot define a const and assign its value after.