C instructions are the commands in the program that instructs the compiler to do certain action. Basically it gives the instruction to the compiler on how to achieve the goal of the program. For example, when we say add two numbers; C compiler will not understand how to do it. But if we write the same in terms of code / command using arithmetic operator ‘+’ and numbers or variables, it gives meaningful information to the compiler on how to get the sum.
Table of Contents
There are three types of instructions in C.
Type Declaration Instructions
These instructions inform the compiler about the type of variables used. That means, whenever a variable is used in the program, we have to specify what types of data it can hold – like integer, float, double, character etc. This makes the compiler to store only those specific types of values in it. For example, a variable intVar1 declared as int will accept and store only integer values. It will never accept float or character values.
Declaration of variable is done at the beginning, inside the main function. This function is the starting point of any C program and all the variables used with main function needs to be declared at the beginning of it, before using it in the function. Any other functions in the program can also have variables and they needs to be declared at the beginning of respective functions.
…. void main() { int intVar1, intVar2, intSum; float flAvg; char chrArr [10]; …. } int subtractNum(int var1, int var2){ int intResult; …. }
Arithmetic Instructions
These instructions are used to perform some arithmetic calculation within the program. It uses arithmetic operators like +, -, X, /, %, =, ++,–, +=,-+ etc. The variables that participate in the arithmetic operations are termed as operands. These instructions can have any simple to complex arithmetic calculations using these symbols.
sum = var1+var2+var3; result = var1+var2/var3; result = (var1+var2)/var3; result = (var1*100) + var2 - var3; result = a+5*30+sum/ 20
These operators have their own precedence while evaluating the instructions. It first evaluates any instructions within parenthesis, (), then multiplication and division, then addition and subtraction and finally assigns the value to the resultant variable.
() → * / → + – → =
If the instruction contains any logical operation, then it evaluates logical NOT (!) first. Then it proceeds to evaluate multiplication/division/modulus (%), then addition/subtraction, then relational operators (==, !=), then logical AND, then logical OR and finally assigns the value.
NOT → * / % → + – → == != → AND → OR → =
Control Instructions
Like the name suggests, these instructions are used to control the flow of the program execution. They maintain certain order in which program needs to be executed. These order of execution may be based on certain conditions or may be based on certain values – may be input values or some result values.
There are four types of control instructions in C.
Sequence Control Instructions
These instructions are responsible for executing the instructions one after the other, as they appear in the program. There are not any checks on the condition or values to control the execution.
For example, a normal program in which all the arithmetic operation like addition, subtraction, multiplication and division is carried out one after the other. Here there is no check on user option, or any other conditions. It will display results of all the operations in the order they appear in the program.
Decision / Selection Control Instructions
This will have set of conditions to execute the instructions within it. If the condition is true, then it will execute the instructions, else it will execute other set of instructions. This type of instructions use if or while statements to make the decision.
For example, suppose user enters the options like A, S, M or D to indicate addition, subtraction, multiplication and division. Depending on the option entered by the user, the program may add, subtract, multiply or divide the numbers. Here it will not execute all the operation but only one of them entered by the user.
Loop Control Instructions
There will be certain need for executing a set of instruction for certain number of times. This is done by using loop control statement. These loops will have limited number of iterations or will have certain conditions which in turn will give iterations for the instructions to be executed. These instructions will use for loops or while or do/while statements to get the loop iterations.
For example, suppose we have display the first 5 entries of name in the array. This will use for loop to iterate through the array from its beginning till 5th entry to display the names in it. Similarly, suppose we need to display all names in sorted array where average marks are not more than 35%. This will use do/while loop to iterate through the array (here we do not know the exact number of iterations) until the condition is matched.
Case Control Instructions
This is similar to decision control instructions where condition will be checked for the execution of the instructions. But this instructions will use switch case statements to check the conditions.
switch (chrColor){ case'B': printf("This is a BLUE color!"); break; case 'R': printf("This is a RED color!"); break; default: printf("This is a NO color!"); }
These control instructions cannot be executed without control statements. There are 4 types of control statements :
- If..Else statement or If..Else If..Else statement
- While loop or Do/While loop statements
- Switch Case statements
- For Loop Statements
These statements help the instructions to get executed accordingly. More details about these statements will be discussed in subsequent topics.