-->

5. C-Operators

C-Instructors: Previous                                                        Next: C-If-Else-Statement

Operators are special symbol that are used to perform Arithmetic operation on variables”.  

It is an essential element of an expression. Suppose we are writing a + b, in this a and b are operands and ‘+’ is operator and the whole makes an Expression.
Category of operators according to number of operands: -
(A)Unary  (B)   Binary   (C)   Ternary

  • Unary: - When we have a single operand in expression. E.g.: - increment/decrement operators (a++).
  • Binary: - when we have two operands in expression. E.g.: - A + B
  • Ternary: - when we have three operands in expression. E.g.: - A? B: C

Types of Operators: - 

Mainly there are 8 operators which are supported by C.

  1. Arithmetic operator.
  2. Relational operator.
  3. Logical operator.
  4. Bitwise operator.
  5. Increment/Decrement operator.
  6. Assignment operator.
  7. Conditional operator.
  8. Special operator.
  1. Arithmetic operator: - These operators are used to perform Arithmetic calculation on operands. The operands can be a variable or an constant.
OperatorSymbolExpression
Addition+x + y
Subtraction-x – y
Multiplication*x * y
Division/x / y
Modulus%x % y

E.g.: - 10 + 20 = 30 20 - 10 = 10 
10 * 5 = 5012 / 2 = 6 
11 % 5 = 2
  1. Relational operator: - As a name implies these operators are used to define the relationship between operands. These operators are used to compare values. An expressions having these operators are known as Relational expression. Relational operators are as follows: -
OperatorOperator nameExample
Less than10 < 20          TRUE
Greater than10 > 20           FALSE
<=Less than or equals to10 <= 10       TRUE
>=Greater than or equals to10 >= 11       TRUE
==Equals to10 == 10       TRUE
!=Not Equal to10 != 10        FALSE


  1. Logical operators: - These are used to combine two or more conditions together to form a single condition. They always returns answer in Boolean i.e. True or False. Three operators are: 
AND (&&)                              OR(||)                       NOT(!)
3.1) AND (&&): - It combines two conditions and return True when both conditions are True, otherwise it returns False. E.g.: -       
10 > 5 (T)                &&          10 < 20 (T)             TRUE
10 < 20 (T)              &&          20 > 10 (F)             FALSE
10 == 20 (F)           &&          20 == 10 (F)            FALSE

3.2) OR ( || ): - It works jut opposite to the AND. It will return true if any one condition is True, otherwise it returns False. E.g.: -       
10 > 5 (T)                ||               10 < 20 (T)             TRUE
10 < 20 (T)              ||               20 < 10 (F)             TRUE
10 == 20 (F)            ||               20 == 10 (F)           FALSE
3.3) NOT ( ! ): -
This is used to reverse the specified condition. If the condition is True then it returns False and if condition returns False then it returns True.
E.g.: -       10 > 5 (T)                                FALSE
                 10 < 5 (F)                                TRUE
Logical operators are divided into If statement and Else If Statement.
(a)     if statement: - these are further divided into following :
  1. simple if
  2. nested if
  3. multiple if
1. simple if: - this instruction contains only a single if. It means single condition with a true statement part. Syntax:

if (condition)
{
True statement part;
}


Example 1 : 
  Write a program to take input from user and check the inputted number is 20 or not?

Solution:
#include<stdio.h>
#include<conio.h>
void main( )
{
                                int num;
                                printf(“Enter a Number : ”);
                                scanf(“%d”, &num);
                                if( num == 20 )
                                {
                                                printf(“Number is 20”);
                                }
}

2. nested if: - In simple if we have only single if with a single True statement, but in nested if we have multiple`s if statements placed inside a if. This can be called as if another if.


If number is Even if number is > 40

  • if number is not equal to 60.

Example 2: Write a program to print a message “Accurate…!!” when the following conditions are satisfied : 
Solution:
#include<stdio.h>
#include<conio.h>
void main( )
{
                                int num;
                                clrscr( );
                                printf(“Enter a Number : ”);
                                scanf(“%d”, &num);
                                if( num > 40 )
                                {
                                                if(num % 2 == 0)
                                                {
                                                                if(num != 60)
                                                                {
                                                                                printf(“Accurate…!!”);
                                                                }
                                                }
                                }
}
clrscr( ): - this function is used to clear the output screen.
Note: When we have a multiple conditions to be satisfied then Logical AND(&&) can be used and makes a program short and simple and also easy to understand. Now take a same example by using AND.
 E.g.:-        Logical AND(&&)
#include<stdio.h>
#include<conio.h>
void main( )
{
                                int num;
                                printf(“Enter a Number : ”);
                                scanf(“%d”, &num);
                                if( num > 40 && num % 2 == 0 && num != 60 )
                                {
                                                printf(“Accurate…!!”);
                                }
}
                                               

E.g.: -       Logical OR( || )
In above example if we change the condition i.e. display message if any one of the condition is True.
#include<stdio.h>
#include<conio.h>
void main( )
{
                                int num;
                                printf(“Enter a Number : ”);
                                scanf(“%d”, &num);
                                if( num > 40 || num % 2 == 0 || num != 60 )
                                {
                                                printf(“Accurate…!!”);
                                }
}
iii.         multiple if: - in nested if, all condition must be satisfied to execute the true part but in multiple if, all condition plays a different role to execute their own true part statement. Difference can be defined is that it contains multiple true part statements depending upon the specified conditions and conditions are independent.
Syntax:
if (condition1)
                {
                                True statement part1;
                }
                if (condition2)
                {
                                True statement part2;
                }
                if (condition3)
                {
                                True statement part3;
                }

E.g.: -       Write a C program to calculate bonus on entered basic salary of an employee. Bonus is to be given according to following dept no.
Dept no                    Bonus
10                            20% of Basic
20                            18% of Basic
30                            15% of Basic
Solution:
 #include<stdio.h>
void main( )
{
                                                float sal;
                                                int dptno;
                                                printf(“Enter a Basic Salary : ”);
                                                scanf(“%f”, &sal);
                                                printf(“Enter a Department Number : ”);
                                                scanf(“%d”, &dptno);
                                                if( dptno == 10 )
                                                {
sal = sal * 1.20 ;
                                                }
                                                if( dptno == 20 )
                                                {
sal = sal * 1.18 ;
                                                }
                                                if( dptno == 30 )
                                                {
sal = sal * 1.15 ;
                                                }
                                                printf(“The Salary is %f ”, sal);
}


C-Instructors: Previous                                                        Next: C-If-Else-Statement