Professional Documents
Culture Documents
Chapter – 3
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Introduction
■ In this chapter, we will discuss
■ Classification of Operators
■ Arithmetic Expression & Evaluation
■ Type Conversion : Implicit & Explicit
■ Associativity & Precedence
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Operators
■ Operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations.
■ Operators are used in programs to manipulate the data and
variables.
■ They usually form a part of the mathematical or logical
expressions.
■ Some operator required 2 operands (Binary Operator) to
perform operation or Some required single operand (Unary)
to perform operation.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
C operators are classified into no. of categories
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increment/Decrement Operators
6. Bitwise Operators
7. Conditional Operators
8. Special Operators
Chapter – 5 –: Constants,
Chapter Variables
3 : Operators & Data Types
and Expression in ‘C’in ‘C’
Classification of Operator Continued…
Arithmetic Operator
■This operator used for numeric calculation.
■There are 2 types of arithmetic operators in C:
1. unary operators : operators that require only one
operand.
2. binary operators : operators that require two operands.
Chapter – 5 –: Constants,
Chapter Variables
3 : Operators & Data Types
and Expression in ‘C’in ‘C’
Classification of Operator Continued…
Unary Operator
C Operation Operator Example
Positive + a = +3
Negative - b = -a
Increment ++ i++
Decrement -- i--
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Binary Operator
C Operation Operator Example
Addition + a+b
Subtraction - a-b
Division / a/b
Multiplication * a*b
Modulus % a%b
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
■ Arithmetic operators are further classified as:
■ Integer Arithmetic
■ 10/5 = 2
■ Real Arithmetic
■ 10.2 + 8.9 = 19.1
■ Mixed Mode
■ 10.5/2 = 5.25
Chapter – 5 –: Constants,
Chapter Variables
3 : Operators & Data Types
and Expression in ‘C’in ‘C’
Classification of Operator Continued…
Relational Operator
■It is use to compared value of two expressions depending on
their relation.
Associativity = Left to right
Operator Example Meaning
> x>y x is greater than y
< x<y x is less than y
>= x>=y x is greater than or
equal to y
<= x<=y x is less than or
equal to y
== x == y equal to
!= x != y not equal to
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Logical Operator
■Operator used with one or more operand and return either value
zero (for false) or one (for true).
■The operand may be constant, variables or expressions.
Associativity examples:
https://www.programiz.com/c-programming/precedence-associativity-operators
Operator Example Meaning
&& a<b && a<c AND
|| a>b || a>c OR
! !(a<c) NOT
■Where logical NOT is a unary operator and other two are binary operator.
Logical AND gives result true if both the conditions are true, otherwise
result is false. And logical OR gives result false if both the condition false,
otherwise result is true.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Assignment Operator
■Assignment operators are used to combine the '=' operator
with one of the binary arithmetic operators.
■Assume c=9;
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
PROGRAM 3- Demonstration of Compound
4 Assignments
■And these operator can only used with the variable, but can't
use with expression and constant as ++6 or ++(x+y+z).
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Increment /Decrement Operator
■It again categories into Prefix Postfix.
Prefix Expression :
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Note
The operand of a prefix expression must be a variable.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Increment /Decrement Operator
■It again categories into Prefix Postfix.
Postfix Expression :
Variable
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Note
(a++) has the same effect as (a = a + 1)
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Increment /Decrement Operator
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Increment /Decrement Operator
■Example :
x = 4;
y = x++;
Results: x = 5, y = 4 (because x is incremented after
assignment)
x = 4;
y = ++x;
Results: x = 5, y = 5 (because x is incremented
before assignment)
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Note
If ++ is after the operand, as in a++, the increment takes
place after the expression is evaluated.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Bitwise Operator
■Bitwise operator permit programmer to access and
manipulate of data at bit level.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Bitwise Operator
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Bitwise Operator
The bitwise not operator (~) invert zeros to ones and ones to
zeros which in effect creates a negative value in binary.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Bitwise Operator
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
#include <stdio.h> c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
main() {
c = a << 2; /* 240 = 1111 0000 */
unsigned int a = 60; /* 60 = 0011 1100 */ printf("Line 5 - Value of c is %d\n", c );
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0; c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
c = a & b; /* 12 = 0000 1100 */ }
printf("Line 1 - Value of c is %d\n", c );
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Unary Operator
■Some Operator required single operand to perform an
operation called Unary Operator.
■Unary operators can use either prefix or postfix notation.
+ a
a +
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Unary Operator
- -op Negative
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Unary Operator
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Unary Operator
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Binary Operator
■Some operator required two operand to perform an operation
called Binary Operator.
■Example : Arithmetic Operator, Relational Operator, Logical
Operator.
a + b
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
PROGRAM 3- Binary
3 Expressions
Note
Both operands of the modulo operator (%) must
be integral types.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Ternary Operator (?:)
■The conditional operator (?:) is used to simplify an if/else
statement.
■Syntax :
Condition ? True Expression1 : False Expression2
■Example :
(n > 1) ? (a + b) : (a * b)
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Short-hand Operator
■A Shorthand operator is a shorter way to express something
that is already available in the C programming language.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Classification of Operator Continued…
Note
The left operand in an assignment expression must be a
single variable.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion
Type Conversion
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion
Type Conversion
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion
Conversion Rank
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion Continued…
Type Conversion
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion Continued…
4. If the expression contains one operand as double data type
and another operand as some other lower data type then the
other operand is also converted to double and the result will be
double. double operator (float(or)long(or)int(or)short)=>double
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion Continued…
Type Conversion
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Type Conversion Continued…
2. Explicit Type Conversion : Explicit type conversions can be
forced in any expression , with a unary operator called a cast.
■Example :
int n = 8;
float x;
x=(float)n; // x will 8.000000 after this
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
PROGRAM 3- Implicit Type Conversion
7
Evaluation of Expressions
■All expressions in parenthesis must be evaluated separately, and
inside out.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Arithmetic Expression & Evaluation
Evaluation of Expressions
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Precedence & Associativity
Precedence of operators
■Operator precedence determines which operator is performed
first in an expression with more than one operators with different
precedence.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
PROGRAM 3- Precedence
5
■For Example : 1 == 2 != 3
■ ((1 == 2) != 3)
i.e, (1 == 2) executes first resulting into 0 (false) then,
(0 != 3) executes resulting into 1 (true) 🡪 Output : 1
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Precedence & Associativity Continued…
Highest
Lowest
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Precedence & Associativity Continued…
Associativity of operators
Left-to-Right Associativity
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
Precedence & Associativity Continued…
Associativity of operators
Right-to-Left Associativity
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
PROGRAM 3- Evaluating
6 Expressions
--a * (3 + b) / 2 - c++ * b
a *4+b/2-c*b --a * 7 / 2 – c++ * b
3*4+4/2–5*4 2*7/2-5*b
12 + 4 / 2 – 5 * 4 2*7/2-5*b
12 + 2 – 5 * 4 2*7/2-5*b
12 + 2 – 20 14 / 2 - 5 * 4
7-5*4
14 - 20
7 - 20
-6 -13
Chapter – 3 : Operators and Expression in ‘C’
Comma operator
■ It can be used to link the related expressions together.
■ A comma-linked list of expressions are evaluated left to
right and the value of right most expression is the value
of the combined expression.
■ Example:
value = (x=10,y=5,x+y);
First assigns the value of 10 to x, then assigns 5 to y and finally
assigns 15 to value
Comma operator has the lowest precedence of all the operators,
the parentheses are necessary.
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
The sizeof operator
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’
3-7 Sample Programs
Chapter
Chapter
– 5 :–Constants,
3 : Operators
Variables
and Expression
& Data Types
in ‘C’in ‘C’