Operators and Expressions
Topics
• Operators and classification
• Expression
• Operator precedence and associativity
• Type conversion
Operators
• Operator: An operator is a symbol that tells the
computer to perform specific mathematical or logical
manipulation.
• Categories of C operators:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Arithmetic Operator
– Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo division
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
Logical Operators
– Operator Meaning
&& logical AND
|| logical OR
! Logical NOT
Op-1 Op-2 Op-1&&Op-2 Op-1||Op-2
0 0 0 0
0 Non-zero 0 1
Non zero 0 0 1
Non zero Non zero 1 1
Assignment Operators
• Assignment operator ‘=‘ is used to assign the
result of an expression to a variable.
• ‘=‘ also can be used in shorthand assignment
operator.
• Ex: x = x + y can be written as x +=y
Increment and Decrement Operators
• Increment operator: ++
• Decrement operator: --
• ++ adds 1 to the operand and -- subtracts 1 from the
operand.
• Difference between x++ and ++x: x++ is post increment
and in case of assignment, the value first assigned and
then increment operand by 1. ++x is pre-increment and
first value of x is incremented by 1 and then the value is
assigned.
if x = 10 and y = x++ then after execution
value of x = 11 and y = 10
similarly, if x=10 and y=++x then after execution value
of x=y=11
• Difference between x-- and --x
Conditional Operator
• The pair ? : is known as conditional operator
• Rule: var = (condition)?exp1:exp2;
• If the condition is true then value of var = exp1
else var = exp2
• Ex: If x = 10, y = 20 and z = (x+10<y)? x*y: y/x ,
the value of z after execution will be 2
Bitwise and Special Operators
• Bitwise operators: &, |, <<, >>, ~
• Special operators: , ;
Arithmetic Expression
• An arithmetic expression is a combination of
variables, constants and operators arranged as
per the syntax of the language.
• Expression are evaluated using an assignment
statement of the form
variable = expression;
• Ex: x = a*b-10;
y = b+c*a;
Operator precedence and Associativity
• Precedence: It is the process of assigning
priority to the operators in the expression.
• High priority: * / %
• Low priority: + -
• Associativity: Operators of the same precedence
are evaluated either from left to right or from
right to left, depending on the level. This is know
as associativity.
• Ex: 5+6-3
Type conversion in Expression
• Consider 7/2 = 3 but if you need the result like
7/2= 3.5 then what you will do???
• The process of converting type of any
expression is called casting.
• The general form: (type_name) expression
• Ex: (float) 7/2 = 3.5

Chap 3(operator expression)

  • 1.
    Operators and Expressions Topics •Operators and classification • Expression • Operator precedence and associativity • Type conversion
  • 2.
    Operators • Operator: Anoperator is a symbol that tells the computer to perform specific mathematical or logical manipulation. • Categories of C operators: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators
  • 3.
    Arithmetic Operator – OperatorMeaning + Addition - Subtraction * Multiplication / Division % Modulo division
  • 4.
    Relational Operators – OperatorMeaning < 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
  • 5.
    Logical Operators – OperatorMeaning && logical AND || logical OR ! Logical NOT Op-1 Op-2 Op-1&&Op-2 Op-1||Op-2 0 0 0 0 0 Non-zero 0 1 Non zero 0 0 1 Non zero Non zero 1 1
  • 6.
    Assignment Operators • Assignmentoperator ‘=‘ is used to assign the result of an expression to a variable. • ‘=‘ also can be used in shorthand assignment operator. • Ex: x = x + y can be written as x +=y
  • 7.
    Increment and DecrementOperators • Increment operator: ++ • Decrement operator: -- • ++ adds 1 to the operand and -- subtracts 1 from the operand. • Difference between x++ and ++x: x++ is post increment and in case of assignment, the value first assigned and then increment operand by 1. ++x is pre-increment and first value of x is incremented by 1 and then the value is assigned. if x = 10 and y = x++ then after execution value of x = 11 and y = 10 similarly, if x=10 and y=++x then after execution value of x=y=11 • Difference between x-- and --x
  • 8.
    Conditional Operator • Thepair ? : is known as conditional operator • Rule: var = (condition)?exp1:exp2; • If the condition is true then value of var = exp1 else var = exp2 • Ex: If x = 10, y = 20 and z = (x+10<y)? x*y: y/x , the value of z after execution will be 2
  • 9.
    Bitwise and SpecialOperators • Bitwise operators: &, |, <<, >>, ~ • Special operators: , ;
  • 10.
    Arithmetic Expression • Anarithmetic expression is a combination of variables, constants and operators arranged as per the syntax of the language. • Expression are evaluated using an assignment statement of the form variable = expression; • Ex: x = a*b-10; y = b+c*a;
  • 11.
    Operator precedence andAssociativity • Precedence: It is the process of assigning priority to the operators in the expression. • High priority: * / % • Low priority: + - • Associativity: Operators of the same precedence are evaluated either from left to right or from right to left, depending on the level. This is know as associativity. • Ex: 5+6-3
  • 12.
    Type conversion inExpression • Consider 7/2 = 3 but if you need the result like 7/2= 3.5 then what you will do??? • The process of converting type of any expression is called casting. • The general form: (type_name) expression • Ex: (float) 7/2 = 3.5