You are on page 1of 30

OPERATORS AND EXPRESSIONS (Chapter 03)

CONTENTS
 OPERATORS
 EXPRESSIONS
 PRECEDENCE
 TYPE CONVERSION IN EXPRESSIONS
 CASTING A VALUE
 MATHEMATICAL FUNCTIONS
OPERATORS

C supports a rich set of operators.

An operator is a symbol that tells the computer to perform


certain mathematical of logical manipulations.

Operators are used in programs to manipulate data and


variables.
CATEGORY OF OPERATORS (8 types)

ARITHMETIC
RELATIONAL
LOGICAL
ASSIGNMENT
INCREMENT AND DECREMENT
CONDITIONAL
BITWISE
SPECIAL
ARITHMETIC OPERATORS

OPERATOR MEANING
+ ADDITION
- SUBTRACTION
* MULTIPLICATION
/ DIVISION
% MODULO DIVISON
KEY POINTS

 Integer division truncates any fractional parts.


 The modulo division produces the remainder of an integer
division.
 The modulo division operator % cannot be used on floating
point data.
 C does not have an operator for exponentiation.
OPERANDS: The data items that operators act upon are
called operands.

EXAMPLE: a+b

Where a and b are variables and are known as operands.


EXAMPLES OF ARITHMETIC OPERATORS

a-b a+b

a*b a/b

a%b -a*b
Suppose that a and b are integer variables whose values are
10 and 3.

expression value

a+b 8

a-b 14

a*b -33

a/b -3

a%b 2
Suppose that a and b are integer variables whose values are
11 and -3.

expression value

a+b 13

a-b 7

a*b 30

a/b 3

a%b 1
KEY POINYS (%)

During modulo division (%) the sign of the result is


always the sign of the first operand.

-14%3 -2

-14%-3 -2

14%3 2
RELATIONAL OPERATORS

OPERATOR MEANING
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
EXAMPLE

4.5<=10 TRUE
4.5<-10 FALSE
-35>=0 FALSE
10<7+5 TRUE
A+B=C+D ?
LOGICAL OPERATORS

OPERATOR MEANING

&& Logical AND

|| Logical OR

! Logical NOT
KEY POINTS

The logical AND ,logical OR operator are used when we


want to test more than one condition and make decision.

a>b && x==10

a>b || x==10
ASSIGNMENT OPERATORS

Assignment operators are used to assign the result of an


expression to a variable. The usual assignment operator is ‘=’

C has a set of ‘shorthand’ assignment operators of the form

v op=exp;

Where v is a variable, op is an arithmetic operator and exp is an


expression.
EXAMPLE
STATEMET EQUIVALENT

v op=exp; v=v op(exp);

STATEMENT SHORTHANT
a=a+1; a+=1;
a=a*(n+1); a*=n+1;
a=a%b; a%=b
INCREMENT AND DECREMENT OPERATORS

OPERATOR MEANING
++ Increment
-- Decrement

KEY POINTS
The operator ++ adds 1 to the operand while --subtracts 1.
EXAMPLE
OPERATOR EQUIVALENT

++m; m++ ;or m=m+1;

--m; m-- ;or m=m-1;


A C program includes an integer variable i whose
initial value is 1.

STATEMENT OUTPUT
printf(“i=%d\n”,i); i=1
printf(“i=%d\n”,++i); i=2
printf(“i=%d\n”,i); i=2

STATEMENT OUTPUT
printf(“i=%d\n”,i); i=1
printf(“i=%d\n”,i++); i=1
printf(“i=%d\n”,i); i=2
CONDITIONAL OPERATOR

General format

exp1? exp2 : exp3;

Where exp1, exp2 and exp3 are expressions.


KEY POINTS

 exp1 is evaluated first. If it is true, then the expression exp2 is


evaluated and becomes the value of the expression.
 If exp1 is false, then the expression exp3 is evaluated
and becomes the value of the expression.

EXAMPLE
A=10;

B=15;

C=(A>B)? A:B;
BITWISE OPERATORS
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
~ One's complement

KEY POINTS: These operators are used for testing the


bits, or shifting them right or left. They are not applied to
float or double.
SPECIAL OPERATORS

OPERATORS SYMBOL
comma ,
sizeof sizeof
pointer & and *
member selection . and ->
EXAMPLE

COMMA OPERATOR

value=(x=10,y=5,x+y);

SIZEOF OPERATOR

m=sizeof(sum);
n=sizeof(long int);
KEY POINT

The sizeof operator intis normally


main(){used to determine the
lengths of the arrays and structures when their sizes are not
known to the program.
printf(“”);
printf(“ ”);
return 0;

}
EXPRESSIONS

Arithmetic expression: An arithmetic expression is a


combination of variables, constants and operators.

ALGEBRIC EXP. C EXP.


aXb-c a*b-c
(m+n) (x+y) (m+n) *(x+y)
ab/c a*b/c
PRECEDENCE of arithmetic operator [ without () ]

An arithmetic expression without parentheses will be evaluated from left


to right using the rules of precedence of operators. There are two
distinct priority levels of arithmetic operators in C
High priority */%
Low priority +-
Example: Suppose a=9,b=12,c=3
x=a-b/3+c*2-1

x=9-12/3+3*2-1 (step 1)
x=9-4+3*2-1 (step 2)
x=9-4+6-1 (step 3)
x=5+6-1 (step 4)
x=11-1 (step 5)
x=10
PRECEDENCE of arithmetic operator [ with () ]

Whenever parentheses are used, the expressions within parentheses


assume highest priority. If two or more parentheses appear one after
another, the expression contained in the left-most set is is evaluated first
and the right- most in the last. Example is given below:

Example: Suppose a=9,b=12,c=3


x=9-12/(3+3)*(2-1)
x=9-12/6*(2-1) (step 1)
x=9-12/6*1 (step 2)
x=9-2*1 (step 3)
X=9-2 (step 4)
X=7 (step 5)
OPERATOR PRECEDENCE GROUPS
CATEGORY OPERATORS ASSOCIATIVITY
Unary -,++,--,!,sizeof,(type) R->L
Arithmetic *,/,% L->R
(mul,div,remain.)
Arithmetic(add,sub) +,- L->R
Relational <,<=,>,>= L->R
Equallity ==,!= L->R
Logical and && L->R
Logical or || L->R
Conditional ?: R->L
assignment =,+=,-=,*=,/=,%= R->L
Operator precedence and associativity:

Definition: Each operator in C has a precedence associated with it. This


precedence is used to determine how an expression involving more than
one operator is evaluated. There are distinct levels of precedence and an
operator may belong to one of the levels. The operators at the higher level
of precedence are evaluated first. The operators of the same precedence
are evaluated either from left to right or from right to left, depending on the
level. This is known as the associativity property of an operator.
Example: consider the following conditional statement:
if(x== 10+15 && y<10)

if(x== 25 && y<10)

y<10 if true
x==25 if false
Because < is higher priority than ==, so y<10 is tested first.

You might also like