Variables #
Variables are one of the fundamental building blocks of our programs. They allow us to store a piece of data in the memory of the computer.
We can update and retrieve the value of a variable whenever we like. The first type of variables that we will be working with are of the int type.
int stands for integers, which in mathematics is a term that represents a whole number. This data type can store both positive and negative numbers,
but it has no understanding of a decimal point, any arithmetic operations that we perform on integer types will result in an integer answer.
Dividing 10 by 3 would give us an answer of exactly 9 when working with integers.
Attributes of a variable #
Variables have five main attributes, two of which we have already mentioned. We will use all attributes of variables but some will be used more frequently than others in the beginning.
The five attributes of a variable are:
- type
- identifier
- value
- size
- memory location
The type of a variable dictates the type of data we can store in it. The identifier is the name we give the variable, this is purely for us humans to use and the compiler and processor don’t actually use them. The value of a variable is the value that it holds. The size of a variable is dictated by its type, and the number of bits and bytes that a type uses have an impact on the range of values that we can store. The memory location is an integer that points to the memory location (like 0xFF83B2C6) where the value is stored, this is mainly used by the computer but we occasionally need to use it explicitly. We will concentrate on the type, identifier, and value for now and we will look at the other attributes later.
How do we declare variables? #
To tell our C compiler that we want to use a variable, we must first declare it. To declare a variable we need two things, the variables type (such as int) and a name (called an identifier) for the variable. Every time we use the identifier, C knows to map the identifier to the memory location so that we don’t have to remember a bunch of hexadecimal numbers.
In the following example, two variables of type int are declared, one named a and one named b:
#include <stdio.h>
int main(void)
{
int a;
int b;
return 0;
}
This allows us to store two numbers, integers, in our program. By default the value of these variables is system dependent and it could be 0, or it could be a garbage value that means nothing. Reading from a variable that has not had a value defined is undefined behavior, it is not defined as part of the C specification and reading from an uninitialized value should be avoided because its behavior can’t be depended upon. On the Raspberry Pi however, these variables are sensibly initialized to 0, on Windows they tend to resemble this: -83424789. If you wrote and tested a program on the Pi and then compiled it on Windows, if you depended on the undefined behavior the results could be quite different!
Initializing variables #
Declaration and initialization are separate steps that can be combined in order to populate our variable with an initial value.
We can simply add an = sign after the variable name, followed by the value to which we want it initialized:
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 5;
return 0;
}
The assignment (=) operator
#
To assign a variable a value, we use the assignment operator. The assignment operator takes the value on the right, and assigns it to the variable on the left. The right hand side can either be a single variable, a constant, or an expression.
The left hand side of the assignment operator must be a modifiable lvalue (left values because they can appear on the left side of the equal sign). An lvalue is an identifier that represents an object in memory, i.e. our variable.
a = 5;
b = 7;
If we write the above statements backwards, they will not work.
The values on the right hand side of the assignment operator are referred to as rvalues (right values because they appear on the right). Constants such as the number 5 and 7 above are rvalues. Our variables are lvalues, but when we use them on the other side of an assignment statement, they become rvalues, as only the value of them is used and there is no assignment made to them. Most variables can be used as both lvalues and rvalues, we’ll expand on those variables that can’t later in the course.
In the following example, a is used on line 1 as an lvalue, and on line 2 as an rvalue:
a = 5;
b = a;
An assignment statement can assign either a single variable, or constant, or it can evaluate an expression and assign the result. An expression is one or more operands joined together by an operator, e.g. arithmetic. The expression on the right hand side of the assignment operator MUST match the type of variable that will be holding the result.
Arithmetic #
C follows the standard BODMAS order of operations, operator precedence, for arithmetic. When we use some more advanced features of C we need to be aware how they fit into the overall operator precedence equation.
BODMAS means that arithmetic operations will be carried out in the following manner:
- Brackets
- Orders/Indices
- Division/Multiplication
- Addition/Subtraction
When operators have equal precedence such as division and multiplication, the operations are carried out left to right, therefor these operators are said to have left-to-right associativity.
In the example below we can see multiple arithmetic operators being used:
int a = 5;
int b = 6;
int c;
c = a * (b + 2);