# Variables

If we want to print "Hello World 1" we can easily do that using what we learned in the previous section. However what if we want to print something like the following


At every loop iteration, we want to increment our number by 1, forever.

We will need to store our number somewhere and then reuse it.

A variable is used in programming to store a value that can change or be reused in the execution of our program.

# Type

We will need to define the variables we want to use in our program. But what type of variable?

In C++ we have to define the type of the variables we are creating. A type can be an integer number (natural number e.g. 1, 5, 200, -20) or a floating-point number (real number e.g. 1.23, 345.11233, -0.1213).

To represent an integer in C++ we use the keyword int and for a floating-point type we use float.

We need to use these terms to tell our program what type of variable we want to create.

# Naming Our Variable

After we declare the type of our variable, we need to give our variable a name so that we can use it in our program.

There are few simple rules for naming a variable in C++:

  • A variable name can consist of any alphabetical letters a to z and A to Z.
  • A variable name can not start with a number, but it can contain any number. For example, myvariable10 is correct for a variable name, but 10myvariable is wrong.
  • You can not use names that are keywords in the C++ language like int or float. These keywords are reserved to be used to identify variable types.
  • C++ is case sensitive. Which means myVariable is not the same as myvariable. The case of each letter matters.
  • Variable names can not have special characters in them except for the underscore character. For example, my_variable is correct but my=variable is not a correct name.

# Declaring a Variable

In our program we can declare a variable by specifying the type and name of the variable and ending.

int myVariable;

This line declares to our program a variable of integer type that is called myVariable.

Now in our program we can use this variable to store a value in it and reuse it.

In the above program, we are declaring our integer variable at line 4. Then at line 6 we are assigning the value 10 to our variable. And at line 8 we are using our variable to pass it to the Serial.println function so we can print the value of our variable.

Try This Out

On line 6 instead of assigning a value 10 to the variable, try assigning the value 1.23.

What is the output?


Our variable is declared to be an integer. So if we store a real number into the integer, only the integer part will be stored. The output you see is 1 since 1.23 was converted into an integer and then stored in our variable.

How can we store 1.23 in a variable and then print it? Try changing int to float on line 4.

# Scope of a Variable

In the above program, try using myVariable inside the loop function. What happens?


void setup() 
 int myVariable;

 myVariable = 10;

void loop() 

We are trying to print our variable every second, similar to an example we did earlier.

If you try running the program, the compiler will output the following error:

/sketch/sketch.ino: In function 'void loop()': 
/sketch/sketch.ino:11:18: error: 
'myVariable' was not declared in this scope Serial.println(myVariable); 

The compiler is telling us that it does not recognize a variable named "myVariable".

C++ has something called scopes. A scope starts with { and ends with }.

The setup function starts a new scope for the lines of code it contains. myVariable is declared inside this scope. The variable can only be used inside the scope it was declared into. This is why inside the loop function, the program does not recognize myVariable.

We can also declare a variable in the global scope of the program. That means our variable can be accessible from anywhere in our program. The global scope would be outside all other scopes. At the beginning of the program before we start with the functions, we can declare our global variables.

In the above program, we are declaring myVariable on the first line, in the global scope. Which allows us to access this variable inside the setup and the loop function.

In the setup we are assigning 1 to our variable. And then in the loop we are printing the variable every 1 second.

# Initializing Variables

We can initialize our variables with a value at the same time of the declaration.

For example int myVariable = 0;

This way myVariable will have the value 0 at the moment it gets created. It is good practice to initialize a variable with a value the moment you declare it.

# Constant Variables

In C++ we can declare variables to be constant. We can use a constant variable in our program if we want to make sure that nothing in our program can change the value of it. This is helpful when you are defining constant values like mathematical constants to be used in your calculations.

To declare a variable to be constant we add const keyword at the beginning of its declaration. Because our variable can not be modified after creating the variable, we have to initialize the variable directly with the value we want.

const float pi = 3.14;


The code below declares a constant floating-point variable pi which is equal to 3.14.

Inside the setup function, we are changing the value of pi and assigning 2 to it. This should not be allowed because pi is constant.

Run the program and see what the compiler tells you about that line of code.

# Next Section

In the next section, we will learn about arithmetic operations so that we can increment our variable by 1 every second.