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.

The constant PI is defined using a define-directive as

#define PI 3.1415927 

The variable area is declared to be of type double, as

    double area; 

This is the case also for the variable radius, which is used, together with the constant PI, to calculate the area as

    area = PI*radius*radius; 

The variable circ is declared to be of type double, as

    double circ; 

It is assigned a value, as the result of the calculation of the circumference as

    circ = PI*2*radius; 

A complete program with variables and assignments is shown in Figure 4.

#include <stdio.h>

#define PI 3.1415927 

int main(void)
{
    char color[] = "blue"; 
    int radius = 3; 
    double area; 
    double circ; 
    area = PI*radius*radius; 
    circ = PI*2*radius; 
    printf("the area is %g\n", area); 
    printf("the circumference is %g\n", circ); 
    printf("the circle is %s\n", color); 
    return 0; 
}

Figure 4. A program with variables and assignments.

This the C view - other views are Java - Python

The program in Figure 4 contains the calculations for area and circumference, as shown above. It also contains statements for printing the results of the calculations.

The program in Figure 4 also contains a string variable named color. The variable color is declared to be an array of characters. It is declared, and initialised to the string "blue", as

    char color[] = "blue"; 

The program in Figure 4 can be compiled, linked, and executed. When running the program, its output becomes

the area is 28.2743
the circumference is 18.8496
the circle is blue