KEMBAR78
Lecture Slide 05 - Operator Expression | PDF | Software Engineering | Mathematical Logic
0% found this document useful (0 votes)
8 views24 pages

Lecture Slide 05 - Operator Expression

The lecture covers operators and expressions in C programming, detailing various types of operators including arithmetic, relational, logical, bitwise, assignment, and special operators. It explains operands, expressions, and statements, providing examples of how these operators function within code. Additionally, it discusses operator precedence, associativity, and type casting to enforce type conversion in variables.

Uploaded by

arifhasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

Lecture Slide 05 - Operator Expression

The lecture covers operators and expressions in C programming, detailing various types of operators including arithmetic, relational, logical, bitwise, assignment, and special operators. It explains operands, expressions, and statements, providing examples of how these operators function within code. Additionally, it discusses operator precedence, associativity, and type casting to enforce type conversion in variables.

Uploaded by

arifhasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 4:

Operator and Expression

Engr. Md Arif Hasan Chowdhury

Assistant Professor
Department of Computer Science and
Engineering
Operator
 is a symbol that tells the compiler to perform a
certain operation (arithmetic, comparison, etc.)
C operators can be classified into the following
types:
 Arithmetic operators
 Relational operators
 Logical operators
 Bitwise operators
 Assignment operators
 Conditional operators
 Special operators
Operand
 What is an Operand?
 Every operator works with some values.
 The value with which an operator works is called
as Operand.
 For example, when we
say 4+5, numbers 4 and 5 are operands
whereas + is an operator.
Expressions and
Statements
 Expression
 Combination of constants,variables,operators, and function calls
 Ex)
a+b
3.0*x – 9.66553
tan(angle)

 Statement
 An expression terminated with a semicolon
 Ex)
sum = x + y + z;
printf(“Dragons!”);
Arithmetic Operators
Example
Operator Description

+ adds two operands (values) a+b


- subtract second operands from first a-b
* multiply two operands a*b
divide the numerator by the denominator, i.e.
/ divide the operand on the left side with the a/b
operand on the right side
This is the modulus operator, it returns the
% remainder of the division of two operands as the a%b
result
This is the Increment operator - increases the
++ integer value by one. This operator needs only a++ or ++a
a single operand.
This is the Decrement operator - decreases
-- integer value by one. This operator needs only --b or b--
a single operand.
Example: Arithmetic
Operator
#include<stdio.h>
int main()
{
int a = 50, b = 23, result;
result = a+b; // addition
printf("Addition of a & b = %d \n",result);
result = a-b; // subtraction
printf("Subtraction of a & b = %d \n",result);
result = a*b; // multiplication
printf("Multiplication of a & b = %d \n",result);
result = a/b; // division
printf("Division of a & b = %d \n",result); return 0;
}
Output:
Addition of a & b = 73
Subtraction of a & b = 27
Multiplication of a & b = 1150
Division of a & b = 2
increment/decrement
 Increment operator ++
 i=i+1;
 i++; // postfix form
 ++i; // prefix form
 decrement operator –
 i=i-1;
 i--; // postfix form
 --I; // prefix form

 Difference between i++ and ++i ?


prefix vs. postfix
 Difference shows up when the operators
are used as a part of a larger expression
 ++k : k is incremented before the expression is
evaluated.
 k++ : k is incremented after the expression is
evaluated.
 Ex) difference?

int a; int b;
int i=0, j=0; int i=0, j=0;
a= (++i) + (++j); b= (i++) + (j++);
Relational operators
Example
Operator Description (a and b, where a = 10
and b = 11)

Check if the two operands


== a == b, returns 0
are equal

Check if the two operands a != b, returns 1


!=
are not equal. because a is not equal to b

Check if the operand on


> the left is greater than the a > b, returns 0
operand on the right

Check operand on the left


< is smaller than the right a < b, returns 1
operand

check left operand is


>= greater than or equal to a >= b, returns 0
the right operand

Check if the operand on


<= the left is smaller than or a <= b, returns 1
equal to the right operand
Example: Relational
Operator
#include <stdio.h>
int main() {
int a = 10, b = 20, result;
result = (a==b); // Equal
printf("(a == b) = %d \n",result);
result = (a<b); // less than Output:
(a == b) = 0
printf("(a < b) = %d \n",result); (a < b) = 1
result = (a>b); // greater than (a > b) = 0
(a <= b) = 1
printf("(a > b) = %d \n",result);
result = (a<=b); // less than
equal to
printf("(a <= b) = %d \n",result);
return 0;
}
Logical Operators
 With the AND operator, only if both operands are true,
the result is true.
 With the OR operator, if a single operand is true, then the result
will be true.
 The NOT operator changes true to false, and false to true.

Example
Operator Description
(a and b, where a = 1 and b = 0)

&& Logical AND a && b, returns 0

|| Logical OR a || b, returns 1

! Logical NOT !a, returns 0


Example: Logical Operator
#include <stdio.h>
int main() {
int a = 1, b = 0, result;
result = (a && b); // And
printf("a && b = %d \
n",result); Output:
(a && b) = 0
result = (a || b); // Or
(a || b) = 1
printf("a || b = %d \ (!a) = 0
n",result);
result = !a; // Not
printf("!a = %d \n",result);
return 0;
}
Bitwise Operators:

•Bitwise operators perform manipulations of data at the bit level.


•These operators also perform the shifting of bits from right to left.
•Bitwise operators are not applied to float or double, long double, void, etc.
•There are 6 bitwise operators in C programming.

Operator Description Example

& Bitwise AND

| Bitwise OR

^ Bitwise Exclusive OR (XOR)

~ One's complement (NOT)

>> Shift right

<< Shift left


Assignment Operators
Example
Operat (a and b are two
Description
or variables, with where
a=10 and b=5)
assigns values from right side operand to
= a=b, a gets value 5
left side operand
adds right operand to the left operand and a+=b, is same as a=a+b,
+=
assign the result to left operand value of a becomes 15
subtracts right operand from the left
a-=b, is same as a=a-b,
-= operand and assign the result to left
value of a becomes 5
operand
mutiply left operand with the right operand a*=b, is same as a=a*b,
*=
and assign the result to left operand value of a becomes 50
divides left operand with the right operand a/=b, is same as a=a/b,
/=
and assign the result to left operand value of a becomes 2
calculate modulus using two operands and a%=b, is same as a=a
%=
assign the result to left operand %b, value of a becomes 0
Example: Assignment
Operator
#include <stdio.h>
int main() {
int a = 10;
int result = a; // Assign
printf("result = %d \n",result);
Output:
result += a; result = 10
printf("result = %d \n",result); result = 20
result = 10
result -= a; // -= operator result = 100
printf("result = %d \n",result);
result *= a; // *= operator
printf("result = %d \n",result);
return 0;
}
Bitwise Operators

shift/
Op. name usage type output
logic
intege Shift bits of a to left by n bit
left shift a<<n
shift r Newly created bits will be 0
op. intege Shift bits of a to right by n bit
right shift a>>n
r Newly created bits will be 0
intege
bit AND a & b AND of a’s and b’s each bit
r
intege
bit OR a | b OR of a’s and b’s each bit
r
bit op.
intege
bit XOR a ^ b XOR of a’s and b’s each bit
r
1’s intege
~a 1’s complement of a
complement r
Truth/False Table

a b a&b a|b a^b a ~a


0 0 0 0 0 0 1
0 1 0 1 1 1 0
1 0 0 1 1
1 1 1 1 0
Bitwise Operators
Examples
 11 = 0000 0000 0000 1011
 17 = 0000 0000 0001 0001

 11 << 2
 0000 0000 0000 1011 << 2 = 0000 0000 0010 1100 = 44

 17 >> 3
 0000 0000 0001 0001 >> 3 = 0000 0000 0000 0010 = 2

output:
11 << 2 = 44
17 >> 3 = 2
example

output:
1f05 & 31a1 = 1101
1f05 | 31a1 = 3fa5
1f05 ^ 31a1 = 2ea4
~1f05 = ffffe0fa
example

expression value result


a 0x1f05 0001 1111 0000 0101

b 0x31a1 0011 0001 1010 0001


~a 0xe0fa 1110 0000 1111 1010

a & b 0x1101 0001 0001 0000 0001

a | b 0x3fa5 0011 1111 1010 0101

a ^ b 0x2ea4 0010 1110 1010 0100


Expression
 Combination of constants,variables linked with
operatorsEx)
a+b
3.0*x – 9.66553
tan(angle)
 Arithmetic expressions
 Uses arithmetic operators

 Can evaluate to any value

 Logical expressions
 Uses relational and logical operators

 Evaluates to 1 or 0 (true or false) only

 Assignment expression
 Uses assignment operators

 Evaluates to value depending on assignment


Precedence , Associativity of
Operators
 Operator Precedence
 determines the order in which operations are performed
 operators with higher precedence are employed first.
precedence operators
1st unary + , unary -
2nd binary * / %
3rd binary + -

 Operator Associativity
 if two operators in an expression have the same
precedence, associativity determines the direction in
which the expression will be evaluated.
* , / , % : L -> R
+ , - (bin): L -> R
= : R -> L
+ , - (unary) : R -> L
Precedence Examples
 Evaluation Order

1 + 2 * 3 - 4
-> 1 + 6 - 4
-> 7 - 4
-> 3

 use parenthesis to force a desired order of


evaluation
 Ex)

(1 + 2) * (3 – 4)
Type casting
 Programmers can enforce type conversion to a variable

Ex1)
double x=3.5;
double y=2.7;
double below_point;

below_point = x*y - (int)(x*y) ;

Ex2)
double x=3.5;
printf(“integer number of x = %d\n”,(int)x);

You might also like