C Instructions

Now that we have written a few programs let us look at the instructions that we used in these programs. There are basically three types of instructions in C:

  1. Type Declaration Instruction
  2. Arithmetic Instruction
  3. Control Instruction

The purpose of each of these instructions is given below:

(a) Type declaration instruction : To declare the type of variables used in a C program.
(b) Arithmetic instruction : To perform arithmetic operations between con-stants and variables.
(c) Control instruction : To control the sequence of execution of various state-ments in a C program.

(a) Type Declaration Instruction

This instruction is used to declare the type of variables being used in the program. Any variable used in the program must be declared before using it in any statement. The type declaration statement is written at the beginning of main( ) function.

int bas;
float rs, grosssal;
char name, code;

There are several subtle variations of the type declaration instruction. These are discussed below:
(a) While declaring the type of variable we can also initialize it as shown below.

int i = 10, j = 25;
float a = 1.5, b = 1.99 + 2.4 * 1.44;

(b)The order in which we define the variables is sometimes important sometimes not. For example,

int i = 10, j = 25;

is same as

int j = 25, j = 10;


float a = 1.5, b = a + 3.1;

is alright, but

float b = a + 3.1, a = 1.5;

is not. This is because here we are trying to use a even before defining it.
(c) The following statements would work.

int a, b, c, d ;
a = b = c = 10 ;

However, the following statement would not work

int a = b = c = d = 10 ;

Once again we are trying to use b (to assign to a) before defining it.

(b) Arithmetic Instruction

A C arithmetic instruction consists of a variable name on the left hand side of = and variable names & constants on the right hand side of =. The variables and constants appearing on the right hand side of = are connected by arithmetic operators like +, -, *, and /.

int ad ;
float kot, deta, alpha, beta, gamma ;
ad = 3200 ;
kot = 0.0056 ;
deta = alpha * beta / gamma + 3.2 * 2 / 5 ;

*, /, -, + are the arithmetic operators.
= is the assignment operator.
2, 5 and 3200 are integer constants.
3.2 and 0.0056 are real constants.
ad is an integer variable.
kot, deta, alpha, beta, gamma are real variables.The variables and constants together are called ‘operands’ that are operated upon by the ‘arithmetic operators’ and the result is assigned, using the assignment operator, to the variable on left-hand side. A C arithmetic statement could be of three types. These are as follows:

(a)Integer mode arithmetic statement – This is an arithmetic statement in which all operands are either integer variables or integer constants. Example

int i, king, issac, noteit ;
i = i + 1 ;
king = issac * 234 + noteit - 7689 ;

(b)Real mode arithmetic statement – This is an arithmetic statement in which all operands are either real constants or real variables. Example :

float qbee, antink, si, prin, anoy, roi ;
qbee = antink + 23.123 / 4.5 * 0.3442 ;
si = prin * anoy * roi / 100.0 ;

Mixed mode arithmetic statement – This is an arithmetic statement in which some of the operands are integers and some of the operands are real. Example

float si, prin, anoy, roi, avg ;
int a, b, c, num ;
si = prin * anoy * roi / 100.0 ;
avg = ( a + b + c + num ) / 4 ;

It is very important to understand how the execution of an arithmetic statement takes place. Firstly, the right hand side is evaluated using constants and the numerical values stored in the variable names. This value is then assigned to the variable on the left-hand side.

Though Arithmetic instructions look simple to use one often commits mistakes in writing them. Let us take a closer look at these statements. Note the following points carefully.
(a) C allows only one variable on left-hand side of =. That is, z = k * l is legal, whereas k * l = z is illegal.
(b) In addition to the division operator C also provides a modular division operator. This operator returns the remainder on dividing one integer with another. Thus the expression 10 / 2 yields 5, whereas, 10 % 2 yields 0. Note that the modulus operator (%) cannot be applied on a float. Also note that on using % the sign of the remainder is always same as the sign of the numerator. Thus –5 % 2 yields –1, whereas, 5 % -2 yields 1.
(c) An arithmetic instruction is often used for storing character constants in character variables.

char a, b, d ;
a = 'F' ;
b = 'G' ;
d = '+' ;

When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory. The ASCII values of ‘F’ and ‘G’ are 70 and 71

(d) Arithmetic operations can be performed on ints, floats and chars.
Thus the statements,

char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z = x + y ;

are perfectly valid, since the addition is performed on the ASCII values of the characters and not on characters themselves. The ASCII values of ‘a’ and ‘b’ are 97 and 98, and hence can definitely be added.
(e) No operator is assumed to be present. It must be written explicitly. In the following example, the multiplication operator after b must be explicitly written.
a = c.d.b(xy) usual arithmetic statement
b = c * d * b * ( x * y ) C statement
Unlike other high level languages, there is no operator for performing exponentiation operation. Thus following statements are invalid.

a = 3 ** 2 ;
b = 3 ^ 2 ;

If we want to do the exponentiation we can get it done this way:

#include <math.h>
main( )
int a ;
a = pow ( 3, 2 ) ;
printf ( “%d”, a ) ;

Here pow( ) function is a standard library function. It is being used to raise 3 to the power of 2. #include is a preprocessor directive. It is being used here to ensure that the pow( ) function works correctly.

(c) Control Instructions in C

As the name suggests the ‘Control Instructions’ enable us to specify the order in which the various instructions in a program are to be executed by the computer. In other words the control instructions determine the ‘flow of control’ in a program. There are four types of control instructions in C. They are:

  1. Sequence Control Instruction
  2. Selection or Decision Control Instruction
  3. Repetition or Loop Control Instruction
  4. Case Control Instruction

The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program. Decision and Case control instructions allow the computer to take a decision as to which instruction is to be executed next. The Loop control instruction helps computer to execute a group of statements repeatedly. In next tutorials we are going to learn these instructions in detail.


You may also like...