08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1 ■ Elementary Programming Concepts

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 book, 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.

Program P1.1 is a C program that requests the user to enter the length of a side and prints the

area of the square:

Program P1.1

#include <stdio.h>

int 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 anything 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 after // are comments that help to explain the program but have

no effect when the program is run. And * is used to denote multiplication.

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

Finally, a program written in a high-level language is usually referred to as a source program

or source code.

7

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!