C Tutorial

C-Program

Let’s understand program with WWH Theory.

What is a program?

Basically, a program is a planned, coordinated group of activities, procedures, etc., often for a specific purpose.

Why is it required?

In last post, we understand about statements (Declaration and Execution statements). As we know we need lot of statements (Computer instructions) to complete a task. So, first big task is to write all those statements and suppose we writes all statements, it is not confirmed that we will get the expected result. So, writing statements is not enough, the statements should be written in some order or as per logic so that we can achieve expected outcome. Let’s understand this through an example:

Suppose we want to do a compound calculation like below:

Exp = a * b + c * d; Let’s say a = 5, b = 2, c = 3, d = 4;

Case 1:

  1. Multiply a, b and store result in temporary t1. // Here t1 = 5 * 2; t1 = 10;
  2. Multiply c, d and store the result in temporary t2. // Here t2 = 3 * 4; t2 = 12;
  3. Add t1, t2 and store the result in Exp. // Here Exp = t1+t2 ; Exp = 10 + 12; Exp = 22

With above statements sequence the Exp value is 22.

Case 2:

  1. Multiply a, b and store result in temporary t1. // Here t1 = 5 * 2; t1 = 10;
  2. Add t1, c and store the result in t2. // Here t2 = t1 + c ; t2 = 10 + 3; t2 = 13;
  3. Multiply t2, d and store the result in temporary Exp. // Here Exp = t2 * d; Exp = 13 * 4; Exp = 52;

With above statements sequence the Exp value is 52.

Case 3:

  1. Multiply c, d and store result in temporary t1.t2 // Here t1 = c * d; t1 = 3 * 4; t1 = 12;
  2. Add t1, b and store the result in t2. // Here t2 = t1 + b ; t2 = 12 + 2; t2 = 14;
  3. Multiply t2, a and store the result in temporary Exp. // Her Exp = t2 * a; Exp = 14 * 5; Exp = 70;

With above statements sequence the Exp value is 70.

In above 3 cases, we just change the order of the statements and got different results. But as per BODMAS rule the result should be 22 instead of 52 and 70. So case 1 is the correct sequence of statements.

Hence, we can say that a program consists of the logically arranged statements.

A program must have start and end statements, because by default computer don’t know with which statement it need to start execution stop the execution.

How to write a program in C Language?

In C language, “A program is set of logically written statements to accomplish a specific task.”. Here logically mean that statements are written in a specific order to achieve desired result. The change in order will change the result.

The structure of a program in C is explained below:

Here:

The first line contains “Return Type”, “Program Name” and “Arguments” enclosed in open and curly brackets.

Return Type: It indicates that what type of value the program will return.

Program Name: Every program must have a name, and it is always unique.

Arguments: It indicates that how many and what kind of values are we passing to program.

The contents of a program must be present within open and close curly brackets called block.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Back To Top