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; 

As an example of an assignment of an integer variable, a variable named i can be assigned the value 5, and at the same time being declared as a variable of type int, as

int i = 5; 

Variables of type int can hold values which are whole numbers. Positive numbers as well as negative numbers are allowed. A complete program with variables, and with literal values being assigned to these variables, is shown in Figure 3.

#include <stdio.h>

int i = 5; 
double pi; 

int main(void)
{
    char a = 'b'; 
    pi = 3.1415927; 
    printf("i is %d and pi is %g and a is %c\n", i, pi, a); 
    return 0; 
}

Figure 3. A program with variables, and with assignments of values to these variables.

This the C view - other views are Java - Python

The program in Figure 3 contains the three variables described above, named i, pi, and a. The program also contains an instruction for printing the values of the variables. The printing is done by a call to a function named printf, as

    printf("i is %d and pi is %g and a is %c\n", i, pi, a); 

In this call to the function printf, which is accesible due to the include-directive

#include <stdio.h>

in the beginning of the program, special formatting codes, consisting of a percent-sign followed by a letter, are used. The letters used are d - indicating that the printed value is an integer value, g - indicating that the printed value is a floating point value, and c, indicating that the printed value is a character value.

The program in Figure 3 can be run, as described in Section How to make it run. The output of the program is then shown, as

i is 5 and pi is 3.14159 and a is b