Variables are values declared with a name and type stored in the computer memory. They become very useful to retain and save information . However, they can also become defined without an assigned value. Variables often come in variety of types that you should be aware of:
// Fundamental conceptschar characterint whole_numberdouble number_with_high_precisionfloat number_with_low_precisionbool true_or_falsevoid nothing
double and float are still numbers with decimals.
You will see string collectionOfCharacters in the next section
Usage of the variables:
char single_character
Using string variable
Declaring variables
In C++, variables have to be declared with a type before its assigned with a value. With declaration, it informs the compiler to know how much memory to allocate and interpret the value. The syntax comes as follows: <type> <variable_name>
One line
For example, to define an integer variable named anything,
int anything;
If you want to declare multiple variables of the same type, it has the similar syntax except every identifier is separated by a comma (,), for this example, we will use a different type of variable, char
int a, b, ..., z;
Initializing variables
After declaring variables, you can simply assign the values by adding an equal sign (aka assignment operator) after the variable name (aka identifier) type identifier = any_value;
any_value must follow the variable type or otherwise the code doesn’t work. This is also called a literal.
Common Mistake
Do not assume that initializing the variable names first then assigning the values alogether would work. For example, (DOES NOT WORK)
int a, b, ..., z = value_1, value_2, ...
Initializing multiple variables in one line and assigning them to a value have to each be separated by a comma.
This is the correct format:
int a = value_1, b = value_2, c = value_3, ...
Strings in C++
C++ has many rich sets of compound types, which are mere basic building blocks. An example of one is string class.
string class is a collection of characters, naturally sequences of characters, which can be in a form of words or sentences.
Unlike other variable types, string, a fundamental type, must include the string library before namespace, almost similar if you had to include iostream for cout.
In the example above, string is initialized with a value wrapped in double quotation marks, aka string literal. There are multiple ways of initialization formats that string isn’t limited to one:
string myString = "it's a string!"; // quotation marksstring myString ("it's a string!"); // paranthesesstring myString {"it's a string!"}; // braces
Like any other fundamental data types, string can remain undeclared and change value.
#include <iostream>#include <string>using namespace std;int main() { string myString; myString = "new string"; cout << myString << endl; myString = "a different new string"; cout << myString << endl; return 0;}
Inserting endl after the insertion operator from the variable myString ends the line of the message.
If you recall the previous example, if we didn’t use endl for each string value we set, all the strings will merge in one line.