How to write a C program

We specified the algorithm using English statements. However, these statements are sufficiently ‘computer-oriented’ for a computer program to be written directly from them. Before we do this, let us see how we expect the program to work from the user’s point of view.

First, the program will type the request for the length of a side; we say the program prompts the user to supply data. The screen display might look like this:

Enter length of side:

The computer will then wait for the user to type the length. Suppose the user types 12. The display will look like this:

Enter length of side: 12

The program will then accept (we say read) the number typed, calculate the area and print the result. The display may look like this:

Enter length of side: 12

Area of square is 144

Here we have specified what the output of the program should look like. For instance, there is a blank line between the prompt line and the line that gives the answer; we have also specified the exact form of the answer. This is a simple example of output design. This is necessary since the programmer cannot write the program unless he knows the precise output required.

In order to write the computer program from the algorithm, a suitable programming language must be chosen. We can think of a program as a set of instructions, written in a programming language, which, when executed, will produce a solution to a given problem or perform some specified task.

The major difference between an algorithm and a program is that an algorithm can be written using informal language without having to follow any special rules (though some conventions are usually followed) whereas a program is written in a programming language and must follow all the rules (the syntax rules) of the language. (Similarly, if we wish to write correct English, we must follow the syntax rules of the English language).

In this series, we will be showing you how to write programs in C, the programming language developed by Ken Thompson and Dennis Ritchie of Bell Laboratories, and one of the most popular and widely used today. A program written in a high-level language such as C is usually referred to as a source program or source code.

The C program which requests the user to enter the length of a side and prints the area of the square is shown here:

main() {
int a, s;
printf("Enter length of side: ");
scanf("%d", &s); //store length in s
a = s * s; //calculate area; store in a
printf("\nArea of square is %d\n", a);

It is not too important that you understand everything about this program at this time. But you can observe that a C program has something (a function) called main followed by opening and closing brackets. Between the left brace { and the right brace } we have what is called the body of the function. The statement

int a, s;

is called a declaration. The parts of a line after double slashes ( // )are comments which help to explain the program but have no effect when the program is run. The asterisk ( * ) is used to denote multiplication.

All of these terms will be explained in detail in due course.