You are on page 1of 27

Dr. Balu L.

Parne
Assistant Professor,
CoED, SVNIT Surat.
Introduction
 Operator: A symbol that tells the computer to perform certain
mathematical or logical manipulations.
 C offers rich set of built-in operators
 Arithmetic
 Relational
 Logical
 Assignment
 Increment and Decrement
 Conditional
 Bitwise
 Special
 Expression: Sequence of operators and operands that reduces to a single
value
 E.g. 10+15
CLASSIFICATION:OPERATORS IN C
ARITHMETIC OPERATOR:
 There are three types of arithmetic operators in C:binary,unary,
and ternary.
 Binary operators: C provides five basic arithmetic binary
operators.
 Arithmetic binary operators:
Arithmetic Operators
Operator Meaning

+ Addition or unary plus

- Subtraction or unary minus

* Multiplication

/ Division: integer division truncates any fractional part

% Modulo division: produces remainder of an integer


division
Operands must be integers
Mixed-mode arithmetic
 When one of the operands is real and other is integer, the
expression is called a mixed-mode arithmetic expression
 if either operand is of real type, then only real operation is
performed is result is always a real number
 e.g. 15/10.0 = 1.5
 15/10=1
A Program to illustrate integer arithmetic to convert number of days into
months and days
#include<stdio.h>
int main()
{
int nod, days, months;
printf("Enter total number of days");
scanf("%d",&nod);
months = nod/30;
days = nod % 30;
printf("Total number of months is %d and number of days are
%d", months, days);
return(0);
}
UNARY OPERATION
 Unary operators: The unary ‘–’ operator negates the value of its operand
(clearly, a signed number). A numeric constant is assumed positive unless
it is preceded by the negative operator. That is, there is no unary ‘+’. It is
implicit.
 Unary increment and decrement operators ‘++’ and ‘--’ operators
increment or decrement the value in a variable by 1.
 Basic rules for using ++ and – – operators:
 The operand must be a variable but not a constant or an expression.
 The operator ++ and -- may precede or succeed the operand.

© Oxford University Press 2013. All rights reserved.


Increment and Decrement operators
 Two very useful operators
 ++ and --
 ++ adds 1 to the operand, while -- subtracts 1
 Are unary operators and take the following form
 ++m; or m++; equivalent to m=m+1
 ++m; Increment and then assignment

 m++; Assignment and then Increment

 --m; or m--; equivalent to m=m-1


 --m; decrement and then assignment

 m--; Assignment and then decrement


POSTFIX :
Postfix:
• (a) x = a++;
 First action: store value of a in memory location for variable x.
 Second action: increment value of a by 1 and store result in memory location
for variable a.
• (b) y = b--;
 First action: put value of b in memory location for variable y.
 Second action: decrement value of b by 1 and put result in memory location
for variable b.

© Oxford University Press 2013. All rights reserved.


PREFIX :
Prefix :
• (a) x = ++a;
 First action: increment value of a by 1 and store result in memory location
for variable a.
 Second action: store value of a in memory location for variable x.
• (b) y = --b;
 First action: decrement value of b by 1 and put result in memory location
for variable b.
 Second action: put value of b in memory location for variable y.

© Oxford University Press 2013. All rights reserved.


Assignment operators
 Used to assign the result of an expression to a variable
v = exp;
 Short hand operator
v op= exp; is equivalent to v=v op (exp);
 E.g.
 x +=3;
 x+= (y+1);
 y-=1;
 var *=2;
Assignment operators…
 Short hand operator has three advantages:
 What appears on the left hand site need not be repeated and
therefore it becomes easier to write
 The statement is more concise and easier to read
 The statement is more efficient
 E.g. value[5*j-2]= value[5*j-2]+delta;
 i.e. value[5*j-2] += delta;
Relational operators
 To compare two quantities and depending on their relation, take
certain decisions
 Comparison can be done with relational operators
 A value of relational expression is either 1 or 0 i.e. TRUE or FALSE
 A simple relational expression
ae-1 relational operator ae-2
 E.g.
 4.5 <= 10 yields TRUE
 4.5 < -10 yields FALSE
 -35 >= 0 yields ?
 10 < 7+5 yields ?
 a+b > c+b yields ?
Relational operators
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to
RELATIONAL OPERATORS:
 C provides six relational operators for comparing numeric quantities.
Relational operators evaluate to 1, representing the true outcome, or 0,
representing the false outcome.

© Oxford University Press 2013. All rights reserved.


Logical operators
 When we want to test more than one condition and make decisions
 A logical expression always yields a value or 1 or 0.
re-1 logical operator re-2
 E.g.
 a>b && x==10
 Truth table

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
LOGICAL OPERATORS :
C provides three logical operators for forming logical
expressions. Like the relational operators, logical operators
evaluate to 1 or 0.
 Logical negation is a unary operator that negates the logical value of its
single operand. If its operand is non-zero, it produces 0, and if it is 0, it
produces 1.
 Logical AND produces 0 if one or both its operands evaluate to 0.
Otherwise, it produces 1.
 Logical OR produces 0 if both its operands evaluate to 0. Otherwise, it
produces 1.

© Oxford University Press 2013. All rights reserved.


BITWISE OPERATORS :
 C provides six bitwise operators for manipulating the
individual bits in an integer quantity. Bitwise operators expect
their operands to be integer quantities and treat them as bit
sequences.
 Bitwise negation is a unary operator that complements the bits in its
operands.
 Bitwise AND compares the corresponding bits of its operands and
produces a 1 when both bits are 1, and 0 otherwise.
 Bitwise OR compares the corresponding bits of its operands and
produces a 0 when both bits are 0, and 1 otherwise.
 Bitwise exclusive or compares the corresponding bits of its operands and
produces a 0 when both bits are 1 or both bits are 0, and 1 otherwise.
BITWISE OPERATORS:

© Oxford University Press 2013. All rights reserved.


CONDITIONAL OPERATOR :
 The conditional operator has three expressions.
 It has the general form expression1 ? expression2 : expression3
 First, expression1 is evaluated; it is treated as a logical condition.
 If the result is non-zero, then expression2 is evaluated and its value is
the final result. Otherwise, expression3 is evaluated and its value is the
final result.
 For example,int m = 1, n = 2, min;
min = (m < n ? m : n); /* min is assigned a value 1 */
 In the above example, because m is less than n, m<n
expression evaluates to be true, therefore, min is assigned the
value m, i.e., 1.

© Oxford University Press 2013. All rights reserved.


Conditional Operator:
 A ternary operator pair “? :”
 exp1 ? exp2 : exp3
 Where exp1, exp2 and exp3 are expressions
 exp1 is evaluated first.
 If it is nonzero (true), then the expression exp2 is evaluated and becomes the
value of the expression
 If it is false, then the expression exp3 is evaluated and becomes the value of
the expression
 E.g.
 a=10; b=15; x=(a>b) ? a : b;
 Yields the value of b in variable x
Special operators
 Comma operator
 E.g.
 value = (x=10, y=5, x+y);
 for (n=1, m=10; n<=m; n++, m++)
 The sizeof operator
 Returns the number of bytes the operand occupies
 E.g.
 m=sizeof(sum);

 n=sizeof(long int);

 k=sizeof(235L);
COMMA OPERATOR :
 This operator allows the evaluation of multiple expressions,
separated by the comma, from left to right in order and the
evaluated value of the rightmost expression is accepted as
the final result. The general form of an expression using a
comma operator is
 Expression M = (expression1, expression2, …,expression N);
 where the expressions are evaluated strictly from left to right
and their values discarded, except for the last one, whose
type and value determine the result of the overall expression.

© Oxford University Press 2013. All rights reserved.


SIZE OF OPERATOR:
• C provides a useful operator, sizeof, for calculating the size of any data item or type.
It takes a single operand that may be a type name (e.g., int) or an expression
(e.g.,100) and returns the size of the specified entity in bytes. The outcome is totally
machine-dependent.
 For example:

© Oxford University Press 2013. All rights reserved.


Thank You.

You might also like