How to Write a Program in C for Complete Beginners

How to Write a Program in C for Complete Beginners
Page content

How to write a C program

In C Programming For Beginners - Part 2 we wrote the algorithm for finding the area of a square. We now show how to write the program in C.

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:

#include <stdio.h>     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.

In C Programming for Beginners – Part 4, we continue to explain the other stages of the programming process.

References

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int