Operators
Function
Algorithm
Flowchart
1
Introduction
An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations. Operators are
used in programs to manipulate data and variables.
C operators can be classified into a number of categories.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
2
Relational Operators
• The relational operators in C are :
Used to compare values of two expressions
depending on their relations.
Operator Meaning
< less that
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
3
Relational Operators
A relational expression yields a value of 1 or 0.
5 < 6 TRUE 1
4.5<10 TRUE 1
-34 + 8 > 23 - 5 FALSE 0
4.5<-10 FALSE 0
5==0 FALSE 0
Arithmetic operators have higher priority over relational
operators.
the associativity of relational operators is left right
4
Relational Operators
The relational operators >, >=, < and <=
have the same precedence. Just below them
in precedence are the equality operators: = =
and !=.
Relational operators have lower precedence
than arithmetic operators , so an expression
like i < lim-1 is taken as i < (lim-1).
3 >= 2 = = -4 < 0
5
Operator
An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations. Operators are
used in programs to manipulate data and variables.
C operators can be classified into a number of categories.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
6
Logical operators
C has the following three logical operators
&& meaning logical AND ( binary operator )
|| meaning logical OR ( binary operator )
! meaning logical NOT( unary operator )
Expressions connected by && or || are evaluated
left to right, and evaluation stops as soon as the
truth or falsehood of the result is known.
7
Logical operators
The logical operators && and|| are used when we want
to test more than one condition and make decisions.
An example is ;
a>b && x==10
An expression of this kind, which combines two or more
relations expressions, is termed as a logical expression or
compound relational expression.
The logical expression given above is TRUE only if a>b
is true and x==10 is true. If either (or both) of them are
false, the expression is false.
8
Logical operators
The precedence of && is higher than that of
||, and both are lower than relational
operators, so
3 < 5 && -3 < -5 || 3 > 2
to decide whether a year is leap year,
year% 4 ==0 && year % 100 != 0 || year % 400 = = 0
9
Finding a Leap Year
There is a leap year every year whose number is
perfectly divisible by four - except for years
which are both divisible by 100 and not divisible
by 400. The second part of the rule effects
century years. For example; the century years
1600 and 2000 are leap years, but the century
years 1700, 1800, and 1900 are not.
to decide whether a year is leap year,
year% 4 ==0 && year % 100 != 0 || year % 400 = = 0
10
Assignment operators
Assignment operators are used to assign the result of an
expression to a variable.
C has a set of ‘shorthand’ assignment operators of the form
v op= exp;
where v is a variable, exp is an expression and op is a C binary
arithmetic operator. The operator op= is known as the shorthand
assignment operator.
The assignment statement v op= exp; is equivalent to v = v op
(exp) ;
For example, x += y + 1; This is same as the statement x = x + ( y
+1);
11
Assignment operators
Statement with simple Statement with shorthand
assignment operator operator
a = a+1 a += 1
a = a-1 a -= 1
a = a * (n+1) a *= n+1
a = a / (n+1) a /= n+1
a=a%b a %= b
12
Assignment operators
The use of shorthand assignment operators
has three advantages:
1. What appears on the left-hand side need not be
repeated and therefore it becomes easier to write.
2. The statement is more concise and easier to read.
3. The statement is more efficient.
13
Increment and decrement operators
C provides two unusual operators for incrementing
and decrementing variables.
The increment operator ++ adds 1 to its operand,
while the decrement operator -- subtracts 1.
The unusual aspect is that ++ and -- may be used
either as prefix operators (before the variable, as in +
+n), or postfix operators (after the variable: n++).
In both cases, the effect is to increment n. But the
expression ++n increments n before its value is used,
while n++ increments n after its value has been used.
14
Increment and decrement operators
Prefix Increment/Decrement:
The statement y=++x means first increment the value of x by
1, then assign the value to y. this statement belongs to the
following two statement: x=x+1; x=x-1;
y=x; y=x;
#include<stdio.h> Output
main()
{
int x=8; x=8
printf(“x=%d\n”,x);
x=9
printf(“x=%d\n”,++x);
x=9
printf(“x=%d\n”,x);
printf(“x=%d\n”,--x);
x=8
printf(“x=%d”,x); x=8
}
15
Increment and decrement operators
Postfix Increment/Decrement:
The statement y=x++ means first the value of x is assign to
y and then x is incremented. this statement belongs to the
following two statement: y=x; y=x;
x=x+1; x=x-1;
#include<stdio.h> Output
main()
{
int x=8; x=8
printf(“x=%d\n”,x);
x=8
printf(“x=%d\n”,x++);
x=9
printf(“x=%d\n”,x);
printf(“x=%d\n”,x--);
x=9
printf(“x=%d”,x); x=8
}
16
17
Mathematical functions
Mathematical functions such as cos, sqrt, log, etc.
are frequently used in analysis of real-life
problems.
We should include the line in the beginning of the
program.
#include<math.h>
18
19
20
21
22
23
Mathematical functions
for example
the roots of ax2+bx+c =0 are
b b 2 4 ac
x 2a
x1= ( -b + sqrt ( b*b - 4*a*c ) ) / ( 2 * a )
x2= ( -b – sqrt ( b*b - 4*a*c ) ) / ( 2 * a )
Solution of the quadratic equation
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c,discriminant,root1,root2;
printf("Input values of a,b, and c\n");
scanf("%f %f %f",&a,&b,&c);
discriminant= b*b-4*a*c;
if(discriminant<0)
printf("\n\nRoots are imaginary\n");
else
{
root1=(-b+sqrt(discriminant))/(2.0*a);
root2=(-b-sqrt(discriminant))/(2*a);
printf("\n\nRoot1=%5.2f\n\nRoots=%5.2f\n",root1,root2);
}
}
24