C Programming language

Types of Instructions in 'C'
Previous Home Next

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

  1. Type Declaration Instruction: To declare the type of variables .
  2. Arithmetic instruction: To perform arithmetic operations between constants and variables.
  3. Control instruction: To control the sequence of execution of various statements.
  1. An arithmetic operation between integer and integer is always integer.
  2. An operation between real and real is always real.
  3. An operation between integer and real is always real.Type Conversion in Assignments.

The type of the expression and the type of thevariable on the left-hand side of the assignment operator will notbe 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 equal(=).

For example, consider the following assignment statements.


int i ;
float b ;
i = 6.3 ;
b = 60 ;

In the example above i is type of integer and b is float type but the value assigned is opposite to each other.i is having 6.3 which is float and after assigning this value, it will contain only 6 rest of .3 is demoted.and variable b is having 60 which is integer type and it assign 60.000000, promoted the value.

Arithmetic operations are performed in an arithmetic expression is called as Hierarchy of operations.and if a=4,b=7,c=3 then using the same expression we will obtain two different answers as z=5.6 or z=7.

To avoid this condition we must be aware of hierarchy of operations.In arithmetic expressions scanning is always done from left to right.

The priority of operations is as shown below

PriorityOperators
FirstParentheses or brackets()
SecondMultiplication & Division
ThirdAddition & Subtraction
FourthAssignment i.e.=

If we consider the logical operators used in C language, the operator precedence will be like

OperatorsType
!Logical NOT ()
* / %Arithmetic and modulus
+ -Arithmetic
== !=Relational
&&Logical AND
||
=Assignment

In the tables above, the higher the position of an operator, higher is its priority.

Control Instructions in C

The ‘Control Instructions’ enable us to specify the order in which instructions in a program are to be executed. or the control instructions determine the ‘flow of control’ in a program.

There are four types of control instructions in C.

(a) Sequence Control Instruction

The Sequence control instruction ensures that the instructions are executed in the same order in which they appear in the program.

(b) Selection or Decision Control Instruction

Decision instructions allow the computer to take a decision as to which instruction is to be executed next.

(c) Repetition or Loop Control Instruction

The Loop control instruction helps computer to execute a group of statements repeatedly.

(d) Case Control Instruction

same as decision control instruction.

Control Statement

There are 3 types of control statement:

  1. If Statement
  2. If-else Statement
  3. The Conditional Operators

If Condition:

If (condition is true) execute the statement; The if condition tells the compiler that the condition is true, execute the statement. Condition of if is always within the pair of parentheses. If condition is true the statement will execute and condition is false, the statement will not execute. For checking condition is true or false we use the relational operators.

Relational Operators:

The ExpressionIs True If
x == y x is equal to y
x!=y x is not equal to y
x<yx is less than y
x>yx is greater than y
x"<=y x is less than or equal to y
x>>=y x is greater than or equal to y

Example


main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}

Multiple statements within if

We can execute more than one statements for that if condition should be satisfied.

If-Else Statement

We can use If – Else condition when we have more than one conditions. If block checks the condition 1 is true it will execute other else block condition will be executed.Or You can say we can execute group of statements by the If – else condition.

Example


/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}

Nested If else

We can use several If – else condition within if block or else block. This is called nested if else condition.

Example:


/* program of nested if-else */
main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ;
scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "You would go to heaven !" ) ;
else
{
if ( i == 2 )
printf ( "Hell was created with you in mind" ) ;
else
printf ( "How about mother earth !" ) ;
}
}

Logical operators

There are only three logical operator in C.

  1. && AND
  2. || OR
  3. ! NOT

The first two operator allows two or more conditions to be combined in an if statement.

Example


main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
}

Loops:-

Loops are required to perform the same series of actions. We frequently need to perform an action over and over. Ability to perform a set of instructions repeatedly called loops.

There are three types of loops

  1. for
  2. while
  3. do while
  4. 1. for loop:

    
    for (initialization_expression; loop_condition; increment_expression){
    // statements
    
    

    There are three parts which is separated by semi-colons in control block of the for loop.

    initialization_expression is executed before execution of the loop starts. This is typically used to initialize a counter for the number of loop iterations. You can initialize a counter for the loop in this part.

    The execution of the loop continues until the loop_condition is false. This expression is checked at the beginning of each loop iteration.

    The increment_expression, is usually used to increment the loop counter. This is executed at the end of each loop iteration.

    Here is an example of using for loop statement to print an integer five times

    
    #include <stdio.h>
    void main(){
    // using for loop statement
    int max = 5;
    int i = 0;
    for(i = 0; i < max;i++){
    printf("%d\n",i);
    }
    }
    
    

    2. While Loop:

    A loop statement allows you to execute a statement or block of statements repeatedly. First it check the condition then execute it. If condition is true it execute otherwise dont execute the statement. Here is syntax of while loop statement:

    
    while (expression) {
    // statements
    }
    Here is a while loop statement demonstration program:
    #include <stdio.h>
    void main(){
    int x = 10;
    int i = 0;
    // using while loop statement
    while(i < x){
    i++;
    printf("%d\n",i);
    }
    // when number 5 found, escape loop body
    int numberFound= 5;
    int j = 1;
    while(j < x){
    if(j == numberFound){
    printf("number found\n");
    break;
    }
    printf("%d...keep finding\n",j);
    j++;
    }
    }
    
    

    Do While Loop:

    do while loop statement allows you to execute code block in loop body at least once. Here is do while loop syntax:

    
    do {
    // statements
    } while (expression);
    Here is an example of using do while loop statement:
    #include <stdio.h>
    void main(){
    int x = 5;
    int i = 0;
    // using do while loop statement
    do{
    i++;
    printf("%d\n",i);
    }while(i < x);
    }
    
    

    The Odd Loop

    Odd loop executed the statements within them a finite number of times. it execute the statements finite times in the loop.

    
    /* Execution of a loop an unknown number of times */
    main( )
    {
    char another ;
    int num ;
    do
    {
    printf ( "Enter a number " ) ;
    scanf ( "%d", &num ) ;
    printf ( "square of %d is %d", num, num * num ) ;
    printf ( "\nWant to enter another number y/n " ) ;
    scanf ( " %c", &another ) ;
    } while ( another == 'y' ) ;
    }
    
    

    Bitwise operations

    OPERATORSOPERATORS NAME
    &AND
    "AND EQUAL TO
    |OR
    |=OR
    ^XOR
    ^=XOR
    ~one's compliment
    «Shift Left
    »Shift Right

    AND OR and XOR

    These require two operands and will perform bit comparisons.

    AND & will copy a bit to the result if it exists in both operands.

    
    /* Execution of a loop an unknown number of times */
    main( )
    {
    char another ;
    int num ;
    do
    {
    printf ( "Enter a number " ) ;
    scanf ( "%d", &num ) ;
    printf ( "square of %d is %d", num, num * num ) ;
    printf ( "\nWant to enter another number y/n " ) ;
    scanf ( " %c", &another ) ;
    } while ( another == 'y' ) ;
    }
    main()
     {
    unsigned int a = 60; /* 60 = 0011 1100 */ 
    unsigned int b = 13; /* 13 = 0000 1101 */
     unsigned int c = 0;          
     c = a & b;   /* 12 = 0000 1100 */
      }
    OR | will copy a bit if it exists in either operand.
    main()
      {
     unsigned int a = 60; /* 60 = 0011 1100 */ 
        unsigned int b = 13; /* 13 = 0000 1101 */
        unsigned int c = 0;          
      c = a | b;   /* 61 = 0011 1101 */
      }
    XOR ^ copies the bit if it is set in one operand (but not both).
    main()
     {
     unsigned int a = 60; /* 60 = 0011 1100 */ 
      unsigned int b = 13; /* 13 = 0000 1101 */
        unsigned int c = 0;          
     c = a ^ b;    /* 49 = 0011 0001 */
      }
    
    

    Ones Complement

    This operator is unary (requires one operand) and has the effect of 'flipping' bits.

    
    main()
    {
    unsigned int Value = 4;     /* 4 = 0000 0100 */
    Value = ~ Value;         /* 251 = 1111 1011 */
    }
    
    

    Bit shift.

    The following operators can be used for shifting bits left or right.

    <<,>>,<<=,>>=

    The left operands value is moved left or right by the number of bits specified by the right operand. For example

    
    main()
    {
    unsigned int Value=4;             /* 4 = 0000 0100 */
    unsigned int Shift=2;
    Value = Value << Shift;         /* 16 = 0001 0000 */
    Value <<= Shift;                     /* 64 = 0100 0000 */
    printf("%d\n", Value);             /* Prints 64 */
    }
    
    

    Assignment Operators

    The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.

    Example

    x = a + b

    Here the value of a + b is evaluated and substituted to the variable x.

    In addition, C has a set of shorthand assignment operators of the form.

    var oper = exp;

    Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper = is known as shorthand assignment operator.

    Logical operators

    There are only three logical operator in C.

    1. && AND
    2. || OR
    3. ! NOT

    The first two operator allows two or more conditions to be combined in an if statement.

    Example:

    
    main( )
    {
    int m1, m2, m3, m4, m5, per ;
    printf ( "Enter marks in five subjects " ) ;
    scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
    per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
    if ( per >= 60 )
    printf ( "First division" ) ;
    if ( ( per >= 50 ) && ( per < 60 ) )
    printf ( "Second division" ) ;
    if ( ( per >= 40 ) && ( per < 50 ) )
    printf ( "Third division" ) ;
    if ( per < 40 )
    printf ( "Fail" ) ;
    }
    
    

    Break Statement:

    break statement is used to break any type of loop such as while, do while and for loop. break statement terminates the loop body immediately.

    Example:

    
    #include <stdio.h>
    #define SIZE 10
    void main(){
    // demonstration of using break statement
    int items[SIZE] = {1,3,2,4,5,6,9,7,10,0};
    int number_found = 4,i;
    for(i = 0; i < SIZE;i++){
    if(items[i] == number_found){
    printf("number found at position %d\n",i);
    break;// break the loop
    }
    printf("finding at position %d\n",i);
    }
    
    

    Continue Statement:

    continue statement is used to break current iteration. After continue statement the control returns to the top of the loop test conditions.

    Example continues:

    
    // demonstration of using continue statement
    for(i = 0; i < SIZE;i++){
    if(items[i] != number_found){
    printf("finding at position %d\n",i);
    continue;// break current iteration
    }
    // print number found and break the loop
    printf("number found at position %d\n",i);
    break;
    }
    }
    
    

    Switch Statement:

    The switch statement allows you to select from multiple choices based on a set of fixed values for a given expression. break keyword is used to signal the end of the block. Here are the common switch statement syntax:

    
    switch (expression){
    case value1: /* execute unit of code 1 */
    break;
    case value2: /* execute unit of code 2 */
    break;
    ...
    default: /* execute default action */
    break;
    }
    Here is an example of using C switch statement
    #include <stdio.h>
    const int RED = 1;
    const int GREEN = 2;
    const int BLUE = 3;
    void main(){
    int color = 1;
    printf("Enter an integer to choose a color(red=1,green=2,blue=3):\n");
    scanf("%d",&color);
    switch(color){
    case RED: printf("you chose red color\n");
    break;
    case GREEN:printf("you chose green color\n");
    break;
    case BLUE:printf("you chose blue color\n");
    break;
    default:printf("you did not choose any color\n");
    }
    }
    
    

     

    Goto statement:

    The goto statement is use for a jump from one point to another point within a function.

    Example:

    
    #include <stdio.h>
    #include <conio.h>
    int main() {
    int n = 0;
    loop: ;
    printf("\n%d", n);
    n++;
    if (n<10) {
    goto loop;
    }
    getch();
    return 0;
    }
    
    
    Previous Home Next