uninitialized variables are NULL (nothing)
Initialized variables contain a value
You can create a uninitialized variable and initialize it later
Code:
// create a integer variable
int my_var;
// initialize the variable
my_var = 100;
Then you can use 'my_var' anywhere you want and even change the value.
It's like a piece of paper which is the variable and what you write on the paper is the value.
Now if what you wrote on the paper isn't what you want you clear the paper and write down the new value.
Code:
// create a integer variable
int my_var;
// initialize the variable
my_var = 100;
// change the value
if (my_var == 100) {
my_var = 25;
}
The 'type' tells what kind of variable it is.
int = integer
char = character
bool = boolean (true/false)
float = floating point
etc.
For example the following code throws a error because "error" is not a integer, it is a string.
Code:
int my_var = "error";
To fix this you could use
Code:
char* my_var = "error";
or use
Code:
// C
string my_var = "error";
// C++
string my_var("error");
The * in "char*" means a pointer and a pointer points to a location in memory which will contain thel data.
Since "char" can only contain 1 character char* can contain many and is like
Code:
char my_var[6] = "error";
Where [6] says how "big" it is.
In this case "error" is 5 characters but you must also have a null terminator ('\0') which actualy tells the system that the string has ended.
Code:
my_var[0] = 'e';
my_var[1] = 'r';
my_var[2] = 'r';
my_var[3] = 'o';
my_var[4] = 'r';
my_var[5] = '\0';
char* doesn't tell how "big" where [x] does tell how big it is.
Since this "char" thing is pretty complicated it is adviced to use "string"