You are on page 1of 31

CHAPTER 3

Operators and Expressions

* All topics and points are not included


OPERATORS
Definition : Symbol that tells compiler to perform certain mathematical
or logical manipulations.
Use: To manipulate data and variable 1. Arithmetic operators
2. Relational operators
Part of: Mathematical or Logical expressions
3. Logical operators
Expressions 4. Assignment operators
5. Increment and Decrement
1.10 + 5
operators
2.c = a * b 6. Conditional operators
Operands : 10,15,c,a,b 7. Bitwise operators
8. Special operators
Operators: + , * , =
1. ARITHMETIC OPERATORS
x = 20, y = 10
or Unary Plus
or Unary Minus

Produces remainder of
the integer division
Can’t be used in
floating point data

NOTE: C does NOT have an operator for exponentiation.


1. ARITHMETIC OPERATORS
Integer Arithmetic Division
All operands integer
Expression: integer expression Use %d for
Operation: integer arithmetic printing
int x=14,y=4;
x+y 18
x-y 10
Modulo Division
x*y 56
x/y 3(Fractional part Sign of first
truncated) operand
x%y 2(Remainder)
1. ARITHMETIC OPERATORS
Use %f for
Real Arithmetic
printing
All operands real Division
Expression: real expression
Operation: real arithmetic

float x=14,y=4;
x+y 18.0000000
x-y 10.0000000
x*y 56.0000000 Modulo Division
x/y 3.5 (Gives
Fractional part)
x%y NOT VALID for real
1. ARITHMETIC OPERATORS
Use %f for
Mixed-mode Arithmetic
printing
operands are real and integer Division
Expression: mixed-mode expression
Operation: mixed-mode arithmetic

float x=14; int y=4;


x+y 18.0000000
x-y 10.0000000
x*y 56.0000000 Modulo Division
x/y 3.5 (Gives
Fractional part)
x%y NOT VALID for
mixed-mode also
2. RELATIONAL OPERATORS
1 20 > 10 : True (One )
Compare two quantities 10 < 8 : False (Zero)
Expression: Relational expression
Value of expression one or zero 2 printf("%d",4.5<10); 1
printf("%d",4.5>10); 0

3
1

4
2. RELATIONAL OPERATORS
2. RELATIONAL OPERATORS
3. LOGICAL OPERATORS
Compare more than one condition

Logical or compound relational expression

Expression with two or more relational expressions


Output value one or zero
3. LOGICAL OPERATORS
4. ASSIGNMENT OPERATORS
Use: To assign result of an expression to a variable

‘shorthand’ assignment operator:


5. CONDITIONAL OPERATOR
Ternary operator pair ‘ ? : ’ for conditional expression

False
exp1 is
evaluated
first
True
6. INCREMENT & DECREMENT OPERATOR

Add 1 Subtract 1

m=m+1
m =m-1

m = 5; m = 5;
y = m++; /* POSTFIX */ y = ++m; /* PREFIX */

y=5 1. Assign value y=6 1. Increment value


2. Increment value 2. Assign value
6. INCREMENT & DECREMENT OPERATOR
int R, count = 10;
6. INCREMENT & DECREMENT OPERATOR

int j = 7, m=5, a;

a = j + m++ + 2 a = j + ++m + 2
a = 14, m = 6 a = 15 , m = 6

a = j + m-- + 2 a = j + --m + 2
a = 14 , m = 4 a = 13 , m = 4

POSTFIX PREFIX
1. Evaluate the expression 1. Increment/ Decrement variable
2. Increment/ Decrement variable 2. Evaluate the expression
7. BITWISE OPERATOR
Use: To manipulate data at bit level
Can not be used with float and double, Use with int only
8. SPECIAL OPERATOR
Comma Operator
• Use to link related expressions
• Evaluated from left to right

1.

2. while (c = getchar() , c !=‘A’)

3. t = x , x = y , y = t ;
8. SPECIAL OPERATOR
sizeof operator : compile time operator
• Syntax : sizeof ( operand )
• Returns number of bytes occupied by operands
ARITHMETIC EXPRESSION
Expression: Combination of operators and operands

Arithmetic Expression: Combination of variables, constants, and operators

a = x + y * 10;
Expressions: Evaluated using an assignment statement of the form

variable = expression;
PRECEDENCE OF ARITHMETIC OPERATORS
Arithmetic Expression without parenthesis will be evaluated
from left to right
using the rule of precedence of operators.

a = x + y * 10;
TWO left to right passes through expression

1st: High priority operator applied


2nd : Low priority operator applied
PRECEDENCE OF ARITHMETIC OPERATORS
a = 9, b = 12, c = 3
x = a – b / 3 + c * 2 – 1;

Order of evaluation can be changed by introducing parentheses () in expression


PRECEDENCE OF ARITHMETIC OPERATORS
• 9 – 12 / (3 + 3) * (2 - 1) = 7

• 9 – (12 / (3 + 3) * 2) - 1 = 4
• Nested parentheses
• 9 – ( (12 / 3) + 3 * 2) - 1 = -2 • Innermost evaluated first

RULES for Evaluation of Expression


• First, Parenthesized sub expression from left to right
• If nested parentheses appears, start with innermost
• Expression within parentheses assumes highest priority
• Associativity rule applied when two or more operators of same
precedence appear
TYPE CONVERSION IN EXPRESSIONS

C Allow: mixing of constants and variables of different types


C Convert: Automatically, any intermediate values to proper type for
evaluation
Automatic type conversion = Implicit type conversion

Strict Rules of type conversion

Operands are of different type: LOWER type automatically converted to


HIGHER type before operation proceeds
IMPLICIT TYPE CONVERSION

Final result of expression is converted to type of variable to left of assignment sign


IMPLICIT TYPE CONVERSION

• float to int causes truncation of


the fractional part
• double to float causes rounding of
digits
• long int to int causes dropping of
excess higher order bits
EXPLICIT TYPE CONVERSION
• Type conversion manually
• Local conversion: explicit conversion or casting a value
EXPLICIT TYPE CONVERSION

General form: (type-name) expression


expression may be constant, variable or expression
TYPE CONVERSION IN EXPRESSIONS
PRECEDENCE : Rule to decide order in which different operators
are applied
ASSOCIATIVITY : Rule to decide order in which multiple occurrence of the
same level operators are applied
(Left to Right or Right to Left )

CHAPTER 3 29
MATHEMATICAL FUNCTIONS

• x and y should be declared as


double
• In trigonometric or hyperbolic
function, x and y are in radians
• C89: All function returns a double
• C99 returns float and long double
also
• C99 added more mathematical
functions

• #include<math.h>
• gcc prog1.c -lm

You might also like