Types of Operators in C programming .pdf

324 views 22 slides Mar 17, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Types of Operators in C programming .pdf


Slide Content

C Operators

Types of operators in C

Arithmetic Operators
OperatorName Description Example
+
Addition Adds together two values x + y
-
Subtraction Subtracts one value from anotherx -y
*
MultiplicationMultiplies two values x * y
/
Division Divides one value by another x / y
%
Modulus Returns the division remainderx % y
++
Increment Increases the value of a variable by 1++x
--
Decrement Decreases the value of a variable by 1--x

#include<stdio.h>
int main()
{
inta, b, add, sub, mul, div, rem;
printf("Enter a, b values : ");
scanf("%d%d",&a,&b);// Reading two values
add=a+b; // Addition Operator
sub=a-b; // Subtraction Operator
mul=a*b; // Multiplication Operator
div=a/b; // Division Operator
rem=a%b; // Remainder (Modulo) Operator
printf("Result of addition is=%d\n", add);
printf("Result of subtraction=%d\n", sub);
printf("Result of multiplication is=%d\n", mul);
printf("Result of division is=%d\n", div);
printf("Result of remainder=%d\n",rem);
return 0;
}

Assignment Operators
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x -3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Assignment operators are
used to assign values to
variables.

Comparison
Operators
•Comparison operators are used to
compare two values (or variables)
Operator Name Example
==
Equal to x == y
!=
Not equal x != y
>
Greater than x > y
<
Less than x < y
>=
Greater than or equal to x >= y
<=
Less than or equal to x <= y

Logical
Operators
•Used to determine the logic between
variables or values
OperatorName Description Example
&&
Logical andReturns true if both
statements are true
x < 5 &&x < 10
||
Logical orReturns true if one of the
statements is true
x < 5 || x < 4
!
Logical notReverse the result,
returns false if the result
is true
!(x < 5 && x <
10)

Special Operators

Use of sizeof() operator

Exampleshowing use
Operator

ofRelational
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
return 0;
}

Exampleshowing use
Operator

ofLogical
#include <stdio.h>
int main()
{
int m=40,n=20;
int a=20,p=30;
if (m>n && m !=0)
{printf("&& Operator : Both conditions are true\n");}
if (a>p || p!=20)
{printf("|| Operator : Only one condition is true\n");}
if (!(m>n && m !=0))
{
else
printf("! Operator : Both conditions are true\n");}
{ printf("!Operator:Bothconditionsaretrue."\
"But, status is inverted as false\n");
}
return 0;
}

BIT WISE OPERATORS IN C

These operators are used to perform bit operations.
Decimal values are converted into binary values
which are the sequence of bits and bit wise operators
work on these bits.Followingarebitwiseoperator
1)
2)
3)
4)
5)
6)
&
|
~
^
<<
>>
Bitwise AND
Bitwise OR
Bitwise NOT
XOR
Left Shift
Right Shift

Program showing Bitwise Operator

inC
#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}

CONDITIONAL OR TERNARY
OPERATORS IN C

Conditional operators return one value if condition
is true and returns another value is condition is false.
This operator is also called as ternary operator.
Syntax
Example
:
:
(Condition? true_value: false_value);
(A > 100 ?0 : 1);

Program foruseof Ternary

Operator
#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
return 0;
}

Increment / Decrement
Operators

Increment operators are used to increase the value of
thevariableby oneand decrementoperatorsare
used to decrease the value of the
programs.
Increment operator
variablebyoneinC
++var_name; (or)var_name++;
Decrement operator
--var_name; (or) var_name –-;

Example forincrement

operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d
i++;
",i);
}
return 0;
}

Example forDecrement

operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d
i--;
",i);
}
return 0;
}

Exercises
Arithmetic Operators
i.Write a program to determine whether a number is divisible
by 5 or not
ii.Write a program to determine if a number is even or odd
iii.Write a C program to check whether a given number is
positive or negative
Relational Operators
i.Write a program to give a discount of 10% if amount of items
purchased exceeds Sh5000

Exercise -Logical operators
i.Write a program to check if a person is eligible to vote. The person
must be a Kenyan Citizen and above 18 years
ii.Write a program to implement the following requirements.
Requirement
A bank will offer a customer a loan if they are 21 or over and have an
annual income of at least Sh21,000. The customers age and income are
input in response to user friendly prompts. Write a program that will
output the following
Expected output
•Congratulations you qualify for a loan.
•Unfortunately, we are unable to offer you a loan at this time .

Tags