C Instructions

A program is nothing but a set of instructions. The program behaves as per the instructions that we give in it. Different instructions help us achieve different tasks in a program. In the last chapter we saw how to write simple C programs. In these programs knowingly or unknowingly we have used instructions to achieve the intended purpose of the program. In this chapter we would explore the instructions that we have used in these programs.

Types of Instructions

There are basically three types of instructions in C:

Since, the elementary C programs would usually contain only the type declaration and the arithmetic instructions; we would discuss only these two instructions at this stage. The control instruction would be discussed in detail in the subsequent chapters.

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.

Ex.:

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

There are several subtle variations of the type declaration instruction. These are discussed below:

Arithmetic Instruction

An arithmetic instruction consists of a variable name on the left hand side of = and variable names and 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 /.

Ex.:

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

Here,

*, /, -, + 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’. While executing an arithmetic statement the operands on right hand side are operated upon by the ‘arithmetic operators’ and the result is then assigned, using the assignment operator, to the variable on left-hand side.

An arithmetic statement could be of three types. These are as follows:

Ex.:

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

Ex.:

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

Ex.:

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

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:

Ex:

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 codes are used to represent any character in memory. For example, ASCII codes of ‘F’ and ‘G’ are 01000110 and 01000111. ASCII values are nothing but the decimal equivalent of ASCII codes. Thus ASCII values of ‘F’ and ‘G’ are 70 and 71.

Thus the statements,

char x, y ;
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.

a = c.d.b(xy)
//usual arithmetic statement
a = c * d * b * ( x * y ) //C statement

Here pow( ) function is a standard library function. It is being used to raise 3.0 to the power of 2.0. The pow( ) function works only with real numbers, hence we have used 3.0 and 2.0 instead of 3 and 2.

#include <math.h> is a preprocessor directive. It is being used here to ensure that the pow( ) function works correctly. We would learn more about standard library functions in Chapter 6 and about preprocessor in Chapter 8.

You can explore other mathematical functions like abs( ), sqrt( ), sin( ), cos( ), tan( ), etc., declared in math.h on your own.

Integer and Float Conversions

In order to effectively develop C programs, it will be necessary to understand the rules that are used for the implicit conversion of floating point and integer values in C. These are mentioned below. Note them carefully.

I think a few practical examples shown in Figure 2.1 would put the issue beyond doubt.

Operation Result Operation Result
5 / 2 2 2 / 5 0
5.0 / 2 2.5 2.0 / 5 0.4
5 / 2.0 2.5 2 / 5.0 0.4
5.0 / 2.0 2.5 2.0 / 5.0 0.4

Figure 2.1

Type Conversion in Assignments

It may so happen that the type of the expression on right hand side and the type of the variable on the left-hand side of an assignment operator may not be same. In such a case, the value of the expression is promoted or demoted depending on the type of the variable on left- hand side of =.

For example, consider the following assignment statements.

int i ;
float b ;
i = 3.5 ;
b = 30 ;

Here in the first assignment statement, though the expression’s value is a float (3.5), it cannot be stored in i since it is an int. In such a case, the float is demoted to an int and then its value is stored. Hence what gets stored in i is 3. Exactly opposite happens in the next statement. Here, 30 is promoted to 30.0 and then stored in b, since b being a float variable cannot hold anything except a float value.

Instead of a simple expression used in the above examples, if a complex expression occurs, still the same rules apply. For example, consider the following program fragment.

float a, b, c ;
int s ;
s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ;

Here, in the assignment statement, some operands are ints whereas others are floats. As we know, during evaluation of the expression, the ints would be promoted to floats and the result of the expression would be a float. But when this float value is assigned to s it is again demoted to an int and then stored in s.

Observe the results of the arithmetic statements shown in Figure 2.2. It has been assumed that k is an integer variable and a is a real variable.

Arithmetic Result Arithmetic Result
k = 2 / 9 0 a = 2 / 9 0.0
k = 2.0 / 9 0 a = 2.0 / 9 0.222222
k = 2 / 9.0 0 a = 2 / 9.0 0.222222
k = 2.0 / 9.0 0 a = 2.0 / 9.0 0.222222
k = 9 / 2 4 a = 9 / 2 4.0
k = 9.0 / 2 4 a = 9.0 / 2 4.5
k = 9 / 2.0 4 a = 9 / 2.0 4.5
k = 9.0 / 2.0 4 a = 9.0 / 2.0 4.5

Figure 2.2

Note that though the following statements give the same result, 0, the results are obtained differently.

k = 2 / 9 ; k = 2.0 / 9 ;

In the first statement, since both 2 and 9 are integers, the result is an integer, i.e. 0. This 0 is then assigned to k. In the second statement 9 is promoted to 9.0 and then the division is performed. Division yields 0.222222. However, this cannot be stored in k, k being an int. Hence it gets demoted to 0 and then stored in k.

Hierarchy of Operations

While executing an arithmetic statement that has multiple operators, there might be some issues about their evaluation. For example, does the expression 2 * x - 3 * y correspond to (2x)-(3y) or to 2(x-3y)? Similarly, does A / B * C correspond to A / (B * C) or to (A / B) * C? To answer these questions satisfactorily, one has to understand the ‘hierarchy’ of operations. The priority or precedence in which the

operations in an arithmetic statement are performed is called the hierarchy of operations. The hierarchy of commonly used operators is shown in Figure 2.3.

Priority Operators Description
1st * / % Multiplication, Division, Modular division
2nd + - Addition, Subtraction
3rd = Assignment

Figure 2.3

Now a few tips about usage of operators in general.

A few examples would clarify the issue further.

Example 2.1:

Determine the hierarchy of operations and evaluate the following expression, assuming that i is an integer variable:

i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8;

Stepwise evaluation of this expression is shown below:

i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8 operation: *
i = 1 + 4 / 4 + 8 - 2 + 5 / 8 operation: /
i = 1 + 1+ 8 - 2 + 5 / 8 operation: /
i = 1 + 1 + 8 - 2 + 0 operation: /
i = 2 + 8 - 2 + 0 operation: +
i = 10 - 2 + 0 operation: +
i = 8 + 0 operation: -
i = 8 operation: +

Notethat 6 / 4 gives 1 and not 1.5. This so happens because 6 and 4 both are integers and therefore 6 / 4 must evaluate to an integer. Similarly 5 / 8 evaluates to zero, since 5 and 8 are integers and hence 5 / 8 must return an integer value.

Example 2.2:

Determine the hierarchy of operations and evaluate the following expression, assuming that kk is a float variable:

kk = 3 / 2 * 4 + 3 / 8;

Stepwise evaluation of this expression is shown below:

kk = 3 / 2 * 4 + 3 / 8
kk = 1 * 4 + 3 / 8 operation: /
kk = 4 + 3 / 8 operation: *
kk = 4 + 0 operation: /
kk = 4 operation: +

Notethat 3 / 8 gives zero, again for the same reason mentioned in the previous example.

All operators in C are ranked according to their precedence. And mind you, there are as many as 45 odd operators in C, and these can affect the evaluation of an expression in subtle and unexpected ways if we aren’t careful. Unfortunately, there are no simple rules that one can follow, such as “BODMAS” that tells algebra students in which order does an expression evaluate. We have not encountered many out of these 45 operators, so we won’t pursue the subject of precedence any further here. However, it can be realized at this stage that it would be almost impossible to remember the precedence of all these operators. So a full-fledged list of all operators and their precedence is given in Appendix A. This may sound daunting, but when its contents are absorbed in small bites, it becomes more palatable.

So far we have seen how arithmetic statements written in C are evaluated. But our knowledge would be incomplete unless we know how to convert a general algebraic expression to a C statement. C can handle any complex algebraic expressions with ease. Some examples of algebraic expressions and their equivalent C expressions are shown in Figure 2.4.

Algebraic Expression C Expression

Figure 2.4

Associativity of Operators

When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. All operators in C have either Left to Right associativity or Right to Left associativity. Let us understand this with the help of a few examples.

Consider the expression

a = 3 / 2 * 5 ;

Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. Both enjoy Left to Right associativity. Therefore firstly / operation is done followed by *.

Consider one more expression.

a = b = 3 ;

Here both assignment operators have the same priority. So order of operations is decided using associativity of = operator. = associates from Right to Left. Therefore, second = is performed earlier than first =.

Consider yet another expression.

z = a * b + c / d ;

Here ***** and / enjoys same priority and same associativity (Left to Right). Compiler is free to perform ***** or / operation as per its convenience, since no matter which is performed earlier, the result would be same.

Appendix B gives the associativity of all the operators available in C. Note that the precedence and associativity of all operators is predetermined and we cannot change it.

Control Instructions

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:

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 the following chapters we are going to discuss these instructions in detail.

Summary

Exercise

[A] Point out the errors, if any, in the following C statements:

[B] Evaluate the following expressions and show their hierarchy.

(a) ans = 5 * b * b * x - 3 * a * y * y - 8 * b * b * x + 10 * a * y ;

(a = 3, b = 2, x = 5, y = 4 assume ans to be an int)

(b) res = 4 * a * y / c - a * y / c ;

(a = 4, y = 1, c = 3, assume res to be an int)

(c) s = c + a * y * y / b ;

(a = 2.2, b = 0.0, c = 4.1, y = 3.0, assume s to be an float)

(d) R = x * x + 2 * x + 1 / 2 * x * x + x + 1 ;

(x = 3.5, assume R to be an float)

[C] Indicate the order in which the following expressions would be

evaluated:

[D] What will be the output of the following programs:

-(a)

 # include <stdio.h>
  int main( )
   {
     int i = 2, j = 3, k, l ;
     float a, b ;
     k = i / j * j ;
     l = j / i * i ;
     a = i / j * j ;
     b = j / i * i ;
     printf ( "%d %d %f %f\\n", k, l, a, b ) ;
     return 0 ;
   }

-(b)

 # include <stdio.h>
  int main( )
  {
    int a, b, c, d ;
    a = 2 % 5 ;
    b = -2 % 5 ;
    c = 2 % -5 ;
    d = -2 % -5 ;
    printf ( "a = %d b = %d c = %d d = %d\\n", a, b, c, d );
    return 0;
  }

-(c)

# include <stdio.h>
 int main( )
 {
   float a = 5, b = 2 ;
   int c, d ;
   c = a % b ;
   d = a / 2 ;
   printf ( "%d\\n", d ) ;
   return 0 ;
 }

-(d)

# include <stdio.h>
 int main( )
  {
     printf ( "nn \\n\\n nn\\n" ) ;
     printf ( "nn /n/n nn/n" ) ;
     return 0 ;
  }

-(e)

# include <stdio.h>
 int main( )
  {
     int a, b ;
     printf ( "Enter values of a and b" ) ;
     scanf ( " %d %d ", &a, &b ) ;
     printf ( "a = %d b = %d", a, b ) ;
     return 0 ;
  }

[E] State whether the following statements are True or False:

[F] Fill in the blanks:

[G] Attempt the following:

Hint: r = sqrt ( x2 + y2 ) and tan-1 ( y / x )

D = 3963 cos-1 ( sin L1 sin L2 + cos L1 cos L2 * cos ( G2 – G1 ) )

wcf = 35.74 + 0.6215t + ( 0.4275t - 35.75 ) * v0.16

where t is the temperature and v is the wind velocity. Write a program to receive values of t and v and calculate wind chill factor (wcf).


Classes
Quiz
Videos
References
Books