# Names and values

Programs need to keep track of information when performing their tasks. A program computing your salary needs to keep track of your salary, your tax, and most likely also some information about your bank account. For this purpose, programs often use variables, which can be assigned values.

Variables can be viewed as named storage places, where values of different types can be stored. Among the different types you will find numerical values, such as the number $$\pi$$, but also strings of characters, like the string "Hello, world" that was used in the program in Figure 1.

## Values

A value can be assigned directly to a variable, using an explicit value. Such an explicit value is called a literal. A character literal, in this case the character literal b, can be assigned to a variable with the name a using the assignment

    char a = 'b';


As can be seen, the assigment is done by placing the character b, enclosed in single quotes, at the right hand side of an equals sign. In this assignment, the word char is placed in front of the variable a. This means that the variable is being declared, to be of the type char. A variable of type char can hold values which are characters, such as the letters a to z, but also special signs, like semicolon (;), comma (,), and exclamation mark (!).

A variable can also be assigned a numerical value. There are two kinds of numerical values. One kind is referred to as floating point values, and the other kind is referred to as integer values. A floating point variable represents decimal numbers and an integer variable represents whole numbers. As an example, the decimal value for $$\pi$$, rounded by the programmer to 7 decimals, can be assigned to a variable named pi, using the assignment

    pi = 3.1415927;


In the above assignment, the variable pi is assumed to be declared, before it is being assigned a value. The declararation is done, using the word double to indicate a floating-point variable with double precision, as

double pi;


## Named storage places

The program in Figure 3 contains the three variables i, pi, and a. Each of these variables can hold a value. In this way, a variable can be viewed as a storage place for a value. Since a variable has a name, it can be viewed as a named storage place.

A variable also has a type. As an example, the variable i is an integer variable. The type of a variable determines the type of values that are allowed to be stored. Hence, the variable i can store integer values.

As demonstrated in the program in Figure 3, variables can be assigned literal values. Variables can also be assigned values that are the result of computations. These computations can be performed using other variables, as well as literal values.

As an example, consider a program for calculating the area and the circumference of a circle. As we have learned in school, the area of a circle is computed using the number $$\pi$$ and the radius of the circle. Using the notation $$r$$ for the radius and the notation $$a$$ for the area, the formula reads $$a = \pi r^2 \quad \quad \quad (1)$$ The circumference, here denoted $$c$$, of a circle is calculated using the circle diameter. Using the fact that the circle diamater is the radius multiplied by two, the formula for calculating the circumference reads $$c = \pi \cdot 2 r \quad \quad \quad (2)$$ Calculations corresponding to (1) and (2) can be done in a program, using a variable named area for the area a, a variable named circ for the circumference, and a variable named radius for the radius $$r$$. In addition, a constant named PI can be used, for the purpose of representing $$\pi$$.

A constant can be defined using a define-directive. A define-directive is handled by the C preprocessor.

## Some computations

Computer programs often perform computations. Here we consider some examples illustrating computations.

A program can be used to calculate future values of an investment. Consider an example where an initial sum $$s$$ is invested. It could then be of interest to calculate the future value of the investment. Assuming that the value is increased by $$r$$ percent each year, the value after a number of years can be calculated.

If we do, as is common in mathematics-oriented books, denote the number of years by a variable, for example called n, the value of the investment, after n years, can be calculated as $$s_n = s \left (1 + \frac{r}{100} \right )^n \quad \quad \quad (3)$$ We note that the formula (3) also applies to other situations, for example when calculating your salary after n years, given an annual raise of r percent.

In a program, designed to calculate a person's salary after a given number of years, we can use a variable

    double salary;


to represent the salary. The raise $$r$$, divided by 100, could be represented by a variable, initalised to an annual raise of 5 percent, as

    double raise = 0.05;


Using a variable

    int n_years;


to represent the number of years, the salary after 5 years and after 10 years, starting at an intial salary of 3000, can be computed by first assigning values to the variables salary and n_years, and then performing calculations according to (3). Program code for performing these operations is shown in Figure 5.

    salary = 3000;
n_years = 5;

salary = salary * pow(1 + raise, n_years);
printf("my salary in %d years is %g\n",
n_years, salary);

salary = salary * pow(1 + raise, n_years);
printf("my salary in %d years is %g\n",
2*n_years, salary);


Figure 5. Calculation of salary after 5 and 10 years, given an intial salary and a yearly raise.

This the C view - other views are Java - Python

The program code in Figure 5 uses a function called pow, for the purpose of calculating the exponent in (3). The function pow is part of the C Standard Library.

The program code in Figure 5 can be placed in a program and executed. The result of running the code inside a program becomes

my salary in 5 years is 3828.84
my salary in 10 years is 4886.68