Arithmetic Operators

heartlustΗλεκτρονική - Συσκευές

2 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

73 εμφανίσεις

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Today’s Agenda



Operators and Expressions



Arithmetic Operators



Relational Operators



Logical Operators



Assignment Operators



Increment and Decrement Operators



Conditional Operators



Special Operators


BITS
Pilani, Pilani Campus

Arithmetic Operators

Operator

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulo Division

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Arithmetic Expression



Let int x = 15; int y = 6;

Example:



x
-
y = 9


x+y = 21


x*y = 90


x/y = 2 (decimal part truncated)


x%y = 3 (remainder of the division)


Q. If x and y are declared as float then?

Q. If x is integer and y is float then?



BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

More Examples:

Write a equivalent C expression for given algebraic


expressions.

1.
(a+b)(c+d)

2.
ax
2
+bx+c

3.
p
r
2
+2
p


4.
s = ut + 1/2at
2

5.
T = (m1m2/m1+m2)

BITS
Pilani, Pilani Campus

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

BITS
Pilani, Pilani Campus

Examples:


5 <= 7


TRUE

5 <
-
8


FALSE

11 < 8+4


TRUE

x+y == a+b

TRUE
if value of x+y is equal to value of a+b

-
20 >= 0


FALSE


Q. Are
!(a>b)

and
a<=b

same??

Q. Are
!(a==b)

and
a!=b

same??

BITS
Pilani, Pilani Campus

Logical Operators

Operator

Meaning

&&

Logical AND

||

Logical OR

!

Logical NOT

Used to test more than one condition

Example:

a<b && y==5

Expression is TRUE only if a<b is TRUE

and y==5 is TRUE

If either or both of them are FALSE

then expression becomes FALSE

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Assignment Operators

Simple Assignment Operators

a = a+1

a = a
-
1

a =a*(b+1)

a = a/(b+1)

a = a%b

Shorthand Operators

a+= 1

a
-
= 1

a*=b+1

a/=b+1

a%=b

Short hand statement is more concise

and easier to read

Q. b*=b; is equivalent to??

Caution: x*=y+2 is x=x*(y+2) and not x = x*y+2


BITS
Pilani, Pilani Campus

Increment and Decrement Operators

++ Adds one to the operand

--

Subtracts one from the operand

Both are unary operators


++i is equivalent to: i = i+1

--
i is equivalent to: i = i
-
1

Is ++i and i++ are same??


Increment and Decrement operators can only be
applied to variables. An expression like
(i+j)++ is illegal.





BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

What is Postfix and Prefix?

In postfix the expression is evaluated first using the
original value of the variable and then the variable is
incremented (or decremented) by one.

Ex.

m = n++;


In prefix the variable is incremented (or decremented)
first and then the expression is evaluated using the new
value

of the variable.

Ex.

m =
--
n;





BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Postfix and Prefix Examples (1)


j =10;

i =j++;

What is the values of i and j?


j =10;

i=++j;

What are the values of i and j?


BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Postfix and Prefix Examples (2)


#include <stdio.h>

int main (void)

{


int x, w=20, z=7;


x = ((++z)


(w
--
)) % 5;


printf (“Value of x = %d, w = %d, z = %d”,x, w, z);

}

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Conditional Operator



Also called Ternary operator


Syntax

expression1 ? expression2 : expression3


Example:

a=5;

b=10;

x =(a>b) ? a : b;

What will be the value of x??



BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Special Operators

The
Size of

Operator

Returns the number of bytes the operand occupies

Examples:

p=sizeof(value);

q=sizeof(float);

r=sizeof(long int);

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

The comma operator



Used to link the related expressions together.



Evaluation starts from left to right.



And the value of the right most expression is the value of


the combined expression.

Example:

value = (a = 6, b = 7, c = 8, a+b+c);

Value contains 21

Use of comma operator

for (i = 1, j = 15; i<j; i++, j
--
)



BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Precedence and Associativity?

Operators

have rules that are used to determine how
expressions are evaluated.


Precedence and associativity

deal with the evaluation
order within expressions


Precedence

rules specify the order in which operators of
different precedence level are evaluated


Associativity
rules decides the order in which multiple
occurrences of the same level operator are applied.


BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Rules of Evaluation of Expression

First, parenthesized sub expression from left to right are
evaluated.


If parentheses are nested, the evaluation begins with
the innermost sub expression.


The precedence rule is applied in determining the order
of application of operators in evaluating sub
-
expressions.


The associativity rule is applied when two or more
operators of the same precedence level appear in a
sub expression.

Operators

Associativity

(),[],
-
>, .

L to R

+,
-
, ++,
--
, !, ~, *, &,
sizeof
, (type)

R to L

*, /, %

L to R

Binary plus(+) and Binary minus (
-
)

L to R

<<, >>

L to R

<, <=, >, >=

L to R

==, !=

L to R

&

L to R

^

L to R

|

L to R

&&

L to R

||

L to R

?:

R to L

=, *=, /=, %=, +=,
-
=, &=, ^=

R to L

,

L to R

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Examples: Evaluate the following
expressions

1) z = 2*3/4+4/4+8
-
2+5/7


z = ??

2) y = 4/5*6+2/7+4


y = ??

3) Let int a,b;


float c,d;


a=5;b=7;


c=4.0;d=3.0;


Then compute


x = a/b*(c+d)/a
-
b+c/d

BITS
Pilani, Pilani Campus

Example

What is the output of the following piece of code ??


#include <stdio.h>

int main (void)

{


int a, b=21, c=7;


a = ++c


b
--

% 5;


printf (“Value of a = %d, b = %d, c = %d”,a, b,
c);

}








BITS
Pilani, Pilani Campus

Example

What is the output of the following piece of code ??


#include <stdio.h>

int main (void)

{


int a, b=21, c=7;


a

= b+++++c;
// Error. To remove write a = b++ + ++c


printf (“Value of a = %d, b = %d, c = %d”,a, b,
c);

}



BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Precedence of Arithmetic Operators


Priority can be overruled by parenthesis


(1+2)*3


Expression inside parenthesis are evaluated first
so


(1+2)*3 gives 9



Now consider the expression 1+2
-
3+4
-
5


Note: + and


have the same precedence


Associativity rule is used to determine how to
evaluate the above expression.


Associativity for Arithmetic Operators is L to R


Try: x = 10
-

16/4 + 4 * 3
-

4

BITS
Pilani, Pilani Campus

BITS
Pilani, Pilani Campus

Example





#include <stdio.h>

int main (void)

{


int a=2 , b=7 , c=8 , d;


d = a*=b+=c%2||1;


printf ("a=%d b=%d c=%d d=%d",a,b, c, d);


return (0);

}