4)

Program 3 - Variables and format specifiers

Program 3 Variables and format specifiers Source code - prog3.c
This third program introduces variables, which are found in abundance in every single useful computer program ever made. A good understanding of what variables are, what they're used for and how to use them is vital for any type of programmer. A lot of people new to programming find variables difficult to grasp at first, but they are really simple and eventually will go on to form large portions of programs.

Format specifiers for the printf statement are also covered in this program. These give a way of displaying the contents of variables on the screen.

 

Program code - new parts shown in red

#include <stdio.h>

void main(void)
{

int prognum;
prognum=3;

printf("This is program number
%d",prognum);

}

 

Description of the program code
Again, like the other programs the template is still there, with the only differences being contained in between the open brace { and closing brace } of the program. This program though looks quite a bit different to the previous programs that have been looked at.

The first difference is in the int prognum; line. This line declares a variable called prognum, and specifies that it is an integer ( int ). Before continuing on to describe this program in more detail, a brief as possible description of variables is needed.

A variable can be thought of as a box, which can contain a single piece of information. On the front of the box are two labels, one which gives the box a name ( Variable name ) and the other which describes what sort of information the box contains ( Data type ).

To start with the programmer has to create a box to hold information / data, ie create a virtual box. This is what the line int prognum; does .. creates a box called prognum, and tells it that the box is going to contain an integer ( a whole number in English ).

This whole process is called variable declaration.

You can give a variable any name you like with just a few exceptions and rules to follow. The idea though, is to give it a meaningful name, so that when you come to use it later it is easy to remember. You cannot use spaces in variable names, and like the rest of the C programming language it is case sensitive, so Prognum is different to prognum. You cannot create two variables with the same name, each variable you declare in a program has to have a different name. For now it is best to make a general ruling that all variable names will consist of lower case letters and numbers ( a-z and 0-9 ), that way it won't get confusing.

A variable always has a value, ie the box always contains something, it is never empty, however the programmer controls what each variable contains. The line prognum=3; does this. It stores the value 3 in the box named prognum. Above, it was stated that a variable always contains something, therefore if the programmer does not assign a variable a value what does it contain ? The answer is, it could be anything, it's probably just any old random number.

A quick recap : Variables can each hold a single piece of information, they are given meaningful names, so that the programmer can reference them, and the type of data that each variable holds also has to be specified. Before a variable can be used it has to be declared. There is no effective limit to the number of variables that you can use in a program.

It is possible to assign a variable a value when you declare it, which is a neat short hand way of doing it. For example you may want a variable to hold a persons age, which would be declared and assigned a value as int age=21;

What variables are used for is probably not all together clear yet, and the description of them above doesn't really shed a lot of light on it, but don't worry, there's loads of example programs to come, all which contain variables, and eventually they will be a lot clearer.

The final part of the program displays the contents of the variable we declared as prognum on the screen using a printf statment with a format specifier (%d). Like the escape sequences in the last program, where they all started with backslashes, format specifiers start with % signs. Following the % sign is a letter which describes to the compiler what format to display the variable as. In the example this is d ( signed decimal integer), which means a whole number that can be negative as well as positive.

Notice now also, that after the last " in the printf statement there is a ,prognum sandwiched in. This is indicates the variable name who's contents will be displayed instead of the %d. So the output from the above code looks like :

This is program number 3

 

Summary
Variables are one of the hardest things to describe, they are simply things that are defined to hold data. The data that is held in variables can be displayed, and manipulated easily ( there is more on manipulating variables in the next programs ). All programs need to store data in memory, without this functionality programs wouldn't be of much use.

Format specifers for the printf statement were also covered briefly. The next few programs explain in a lot more detail about variables and different format specifiers. Don't worry if you don't understand the descriptions here, as long as you can sort of see why the code works and do the task below, you are ready to move on a page.

 

Tasks

3.1) Write a program that stores your age in a variable and uses the variable to produce an output similar to the one below :
My age is 21

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001