In our previous C++ Hello World tutorial, we learned how to output “Hello World” to the terminal. Now, we are going to learn how to create variables that contain different types of information in memory so we can manipulate these variables throughout our program.
C++ Variables
Variables are created through the use of identifiers. An identifier is a sequence of one or more digits, letters, or the underscore character. It also may not be a reserved word, have two consecutive underscores, or begin with a digit. A reserved word is simply a keyword that is reserved by the language such as: int, double, string.
Below, is an example of the creation and manipulation of variables.
1 2 3 4 5 6 7 | // Below we are creating two different variables of type int int num = 5; int num2 = 10; // Below we are creating a variable of type in that stores the sum of the // previous two variables. int sum = num + num2; |
C++ Fundamental Data Types
For now, we will discuss a couple of the fundamental data types the C++ language has to offer. Later, we’ll learn how to create our own through the use of classes.
| Variable | Example | Description |
|---|---|---|
| int | 1, 2, 10, 999, -562 | A positive or negative integer value |
| double | 1, 1.2, -1.2 | A floating-point variable |
| bool | true, false | Either true or false, useful for conditions |
| char | a, b, 2, 1 | A single character |
Declaring variables
In order to declare a variable, you need to specify the type of variable, identifier, and initialize it if you wish. Below are a couple of examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Declaring and initializing variables differently int num1 = 5; double num2; num2 = 2.5; // Declaring a list of variables int a, b, c, d; // Setting a boolean value bool val = true; // When you declare a character, you need to use the ''s char e = 'b'; |
Some things to take into consideration are below:
- Uninitialized variables is a bad practice. If you forget, and try to use them later throughout your program they will contain garbage and will lead to undefined behavior for your program
Example of C++ Program with Variables
Below is a simple example of a program that uses what we just learned. Try it out for yourself and try new things as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> using namespace std; int main() { //Outputs to the terminal, followed by a newline character cout << "Welcome!\n"; // Declaring some variables and initializing them int a, b, c; a = 3; b = 2; c = a + b; // Outputting c to the terminal as well as a new line cout << "The sum of a and b is: " << c << endl; return 0; |
Further practice
You need further practice? Don’t worry, in the next few tutorials, we’ll learn how to use the different variable types you learned today. Just as long as you understand the material in this tutorial you are doing fine. Have any questions? Post them below.
In the next tutorial, we’ll be learning about global and constant variables as well as the scope of variables.
