C++ Tutorial: Variables

Variables are an essential part of C++. Like in math, they are essentially placeholders for values, and they are stored in the computer memory. Variables can be defined, their values changed, and deleted. This tutorial will hopefully give you a basic understanding of variables in C++. Now, look at the following line:

int x;

The C++ compiler first sees int, so it knows the variable stores an integer number. It then looks for other variable-modification terms, such as * (we’ll get to those later). It sees none of those, so it now looks for the name of the variable. In this case, it found it, x. It then sees a semicolon, marking the end of the statement. Now it knows that we have declared the variable x. We can now do the following line:

x = 10;

This sets the value of x to be 10. Now, the variable is ready to be accessed. We can access the variable with the following line of code:

cout << x << endl;

This outputs the value of x, which is 10. The entire program will look like this:

#include <iostream>
using namespace std;

int main() {
    int x;
    x = 10;
    cout << x << endl;
    return 0;
}

When you run that program, it will output:

10

We have just used the variable x. We could have also initialized the variable when we were declaring it, like so:

int x = 10;

This eliminates the need for 2 statements to use the variable. Also, the variables are not limited to just integers.

Variable Types

There are several types of variables, including but not limited to the following:

  • char: A character. Characters are actually an int's in disguise, because that is how the computer stores them. char's are enclosed in single quotes ('), like so: char a = 'a'.
  • double: A decimal number, like 22.4. They can be declared normally: double a = 183.12.
  • string: A series of characters, such as “Hello, World!” They are enclosed in double quotes ("), but need the #include <string> preprocessor directive to work. (There is a way to do it without this, but we can get to that later.) We declare/initialize a string like this: string a = "hi there".
  • bool: A boolean value. Can either store true or false. For example: bool a = true.

Variable Modifiers

There are also several keywords to modify the properties of a variable. They tend to go before the variable type like so:

const unsigned int x = 10;

There are also several variable modifiers. Here are a few:

  • const: Makes a variable unchangeable. When using this modifier, the variable must be initialized with the declaration, because it can’t be changed afterwards. C++ will throw an error if it is attempted to be modified. Constant variables’ labels are usually in SCREAMING_SNAKE_CASE. This is an example: const bool IM_A_SCREAMING_SNAKE = true.
  • unsigned: Removes the sign from an integer or number-type object. This makes it so the number cannot be negative. For example, unsigned int b = -10. Although this will not throw an error, it will act very wierdly, in a way that is beyond the scope of this tutorial. More information here.
  • long / short: Makes the variable take up less/more space. Only works on specific variables. For example, on some systems, long int takes up twice as much space as int. However, this is very machine specific, and a program should not rely on the sizes of these modifiers.

Macros

Macros are a special type of variable that do not need any type. Instead, they are a preprocessor directive. Unlike regular variables, macros physically substitute the text of the macro into its place. For example, this is a valid C++ program:

#include <iostream>
#define MAIN_FUNCTION_BEGIN int main() {
#define PRINT_BEGIN cout << 
#define STRING_TO_PRINT "Hello, World!"
#define PRINT_END << endl
#define RETURN_KEYWORD return
#define RETURN_VALUE 0
#define SEMICOLON ;
#define MAIN_FUNCTION_END }
using namespace std;

MAIN_FUNCTION_BEGIN
    PRINT_BEGIN STRING_TO_PRINT PRINT_END SEMICOLON
    RETURN_KEYWORD RETURN_VALUE SEMICOLON
MAIN_FUNCTION_END

Obviously, doing something like that is discouraged, because it makes the program extremely confusing. Another disadvantage with macros is that they are not procedural. They are always made at the beginning of the program. Macros can also be left undefined, which is usually used for configuration:

#define USE_FILE

Macros may be discussed in a later tutorial.