Unit 4 (Operators and Expression)
Unit 4 (Operators and Expression)
Sulav Nepal
Email: [email protected]
Contact No.: 9849194892
Master’s in Computer Information System (MCIS) – Pokhara University
Bachelor of Science. Computer Science & Information Technology (B.Sc. CSIT) – Tribhuwan University
Microsoft Technology Associate (MTA): Windows Server Administration Fundamentals
Microsoft Certified Technology Specialist (MCTS): Windows Server 2008 R2, Server Virtualization
Microsoft Specialist (MS): Programming in HTML5 with JavaScript and CSS3
Microsoft Students Partner (MSP) 2012 for Nepal
P
R
E Syllabus
P
A
R
UNIT 4: Operators and Expressions (4 Hrs.)
E o Arithmetic Operator
D
o Relational Operator
B
Y o Logical or Boolean Operator
o Assignment Operator
S
U o Ternary Operator
L
A o Bitwise Operator
V
o Increment or Decrement Operator
N o Conditional Operator
E
P o Special Operators (sizeof and comma)
A
L o Evaluation of Expression (implicit and explicit type conversion)
o Operator Precedence and Associativity
5/28/2022 5:17 AM
P
R
E
P
A
R
UNIT 4
E
D
B
Y
N
E
P
A
L
5/28/2022 5:21 AM
P
R
E Operators
P
A
R
Symbol that operates on certain data type or data item.
E
D
Used in program to perform certain mathematical or logical
B
Y manipulations.
S
U
L For example: In a simple expression 5+6, the symbol “+” is called an
A
V
operator which operates on two data items 5 and 6.
N
E The data items that operator act upon are called operands.
P
A
L
5/28/2022 5:17 AM
P
R
E Expression
P
A
R
An expression is a combination of variables, constants, and operators
E written according to syntax of the language.
D
B
Y For example:
S 8+10
U
L
a+c*d
A a>b
V
a/c
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E Operators
P
A
R
We can classify operators into:
E Unary Operators
D
Which requires only one operand
B For example: ++, --
Y
S
U
Binary Operators
L Which requires two operands
A
V
For example: +, -, *, /, <, >
N
E Ternary Operators
P
Which requires three operands
A
L For example:“?:” (conditional operator)
5/28/2022 5:17 AM
P
R
E Arithmetic Operators
P
A
R
Assume variable A holds 20 and variable B holds 10, then
E
D Operator Description Example
B + Adds two operands 𝐴 + 𝐵 = 30
Y − Subtracts second operand from the first 𝐴 − 𝐵 = 10
S ∗ Multiplies both operands 𝐴 ∗ 𝐵 = 200
U / Divides numerator by de-numerator 𝐴/𝐵 = 2
L
A % Modulus operator and remainder of after an integer division 𝐴%𝐵 = 0
V
++ Increment operator increases the integer value by one 𝐴 ++= 21
N −− Decrement operator decreases the integer value by one 𝐴 −−= 19
E
P
A
L
5/28/2022 5:17 AM
P
R
E Integer Arithmetic
P
A
R
Division Rule
E
D
𝑖𝑛𝑡/𝑖𝑛𝑡 = 𝑖𝑛𝑡
B
Y
𝑓𝑙𝑜𝑎𝑡/𝑓𝑙𝑜𝑎𝑡 = 𝑓𝑙𝑜𝑎𝑡
S
U
L
A 𝑖𝑛𝑡/𝑓𝑙𝑜𝑎𝑡 = 𝑓𝑙𝑜𝑎𝑡
V
N 𝑓𝑙𝑜𝑎𝑡/𝑖𝑛𝑡 = 𝑓𝑙𝑜𝑎𝑡
E
P
A
L
5/28/2022 5:17 AM
P
R
E EXAMPLE – ONE
P
A #include<stdio.h>
R #include<conio.h>
E void main()
D { int a=21; int b=10; int c;
c=a+b;
B printf("Line 1 -Value of c is %d\n",c);
Y c=a-b;
printf("Line 2 -Value of c is %d\n",c);
S c=a*b;
U printf("Line 3 -Value of c is %d\n",c);
L c=a/b;
A printf("Line 4 -Value of c is %d\n",c);
V c=a%b;
printf("Line 5 -Value of c is %d\n",c);
N a++;
E printf("Line 6 -Value of c is %d\n",a);
P a--;
A printf("Line 7 -Value of c is %d\n",a);
L getch();
}
5/28/2022 5:17 AM
P
R
E Relational Operators
P
A
R
Assume variable A holds 10 and variable B holds 20, then
E Operator Description Example
D
Checks if the values of two operands are equal or not. If yes, then ሺ𝐴 =
==
B the condition becomes true. = 𝐵ሻ𝑖𝑠 𝑛𝑜𝑡 𝑡𝑟𝑢𝑒
Y
Checks if the values of two operands are equal or not. If the
!= 𝐴! = 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
S values are not equal, then the condition becomes true.
U Checks if the value of left operand is greater than the value of
L > 𝐴 > 𝐵 𝑖𝑠 𝑛𝑜𝑡 𝑡𝑟𝑢𝑒
right operand. If yes, then the condition becomes true.
A
V Checks if the value of left operand is less than the value of right
< 𝐴 < 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
operand. If yes, then the condition becomes true.
N Checks if the value of left operand is greater than or equal to the ሺ𝐴 >
E >=
value of right operand. If yes, then the condition becomes true. = 𝐵ሻ 𝑖𝑠 𝑛𝑜𝑡 𝑡𝑟𝑢𝑒
P
A Checks if the value of left operand is less than or equal to the
L
<= ሺ𝐴 <= 𝐵ሻ 𝑖𝑠 𝑡𝑟𝑢𝑒
value of right operand. If yes, then the condition becomes true.
5/28/2022 5:17 AM
P
R
E EXAMPLE – TWO
P
A #include<stdio.h>
#include<conio.h>
R
void main()
E
{ int a=21; int b=10; int c;
D if(a==b)
{ printf("Line 1 - a is equal to b\n"); }
B else
Y { printf("Line 1 - a is not equal to b\n"); }
if(a<b)
S { printf("Line 2 - a is less than b\n"); }
U else
L { printf("Line 2 - a is not less than b\n"); }
A if(a>b)
{ printf("Line 3 - a is greater than b\n"); }
V
else
{ printf("Line 3 - a is not greater than b\n"); }
N if(a<=b)
E { printf("Line 4 - a is either less than or equal to b\n"); }
P if(a>=b)
A { printf("Line 4 - a is either greater than or equal to b\n");}
L getch();
}
5/28/2022 5:17 AM
P
R
E Logical Operators
P
A
R
Assume variable A holds 1 and variable B holds 0, then
E
D Operator Description Example
B
Called “Logical AND Operator”. If both the operands are
&& 𝐴 && 𝐵 𝑖𝑠 𝑓𝑎𝑙𝑠𝑒
Y non-zero, then the condition becomes true.
Called “Logical OR Operator”. If any of the two operands is
S || ሺ𝐴 | 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
non-zero, then the condition becomes true.
U
L Called “Logical NOT Operator”. It is used to reverse the
! 𝐴 && 𝐵 𝑖𝑠 𝑡𝑟𝑢𝑒
A ! logical state of its operand. If a condition is true, then
! 𝐴 || 𝐵 𝑖𝑠 𝑓𝑎𝑙𝑠𝑒
V Logical NOT Operator will make it false.
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E EXAMPLE – THREE
P
A #include<stdio.h>
#include<conio.h>
R
void main()
E
{ int a=5, b=20;
D if(a && b)
{ printf("Line 1 - Condition is true\n"); }
B else
Y { printf("Line 1 - Condition is not true\n"); }
if(a || b)
S { printf("Line 2 - Condition is true\n"); }
U else
L { printf("Line 2 - Condition is not true\n"); }
A // Let us change the value of a and b
a=0; b=10;
V
if(a && b)
{ printf("Line 3 - Condition is true\n"); }
N else
E { printf("Line 3 - Condition is not true\n"); }
P if(!(a && b))
A { printf("Line 4 - Condition is true\n"); }
L getch();
}
5/28/2022 5:17 AM
P
R
E Assignment Operators
P
A
R Operator Description Example
E Simple assignment operator. Assigns values from right side 𝐶 = 𝐴 + 𝐵 will assign
D =
operands to left side operand the value of 𝐴 + 𝐵 to 𝐶
B Add AND assignment operator. It adds the right operand to the 𝐶 += 𝐴 is equivalent to
+=
Y left operand and assign the results to the left operand 𝐶 =𝐶+𝐴
S
Subtract AND assignment operator. It subtracts the right operand 𝐶 −= 𝐴 is equivalent to
−=
U from the left operand and assigns the result to the left operand 𝐶 =𝐶−𝐴
L Multiply AND assignment operator. It multiplies the right
A 𝐶 ∗= 𝐴 is equivalent to
∗= operand with the left operand and assigns the result to the left
V 𝐶 =𝐶∗𝐴
operand
N Divide AND assignment operator. It divides the left operand with 𝐶/= 𝐴 is equivalent to
/=
E the right operand and assigns the result to the left operand 𝐶 = 𝐶/𝐴
P
A
Modulus AND assignment operator. It takes modulus using two 𝐶% = 𝐴 is equivalent to
%=
L operands and assigns the result to the left operand 𝐶 = 𝐶%𝐴
5/28/2022 5:17 AM
P
R
E EXAMPLE – FOUR
P
A #include<stdio.h>
R #include<conio.h>
E void main()
D
{ int a=1, b=2, c;
B c=a+b;
Y printf("Line 1 - c=%d",c);
c+=a;
S
printf("\nLine 2 - c=%d",c);
U
L c-=a;
A printf("\nLine 3 - c=%d",c);
V c*=a;
printf("\nLine 4 - c=%d",c);
N
c/=a;
E
P printf("\nLine 5 - c=%d",c);
A c%=a;
L printf("\nLine 6 - c=%d",c);
getch();
5/28/2022 5:17 AM
}
P
R
E Increment & Decrement Operators
P
A
R Increment operator is used to increase the value of an operand by 1
E
D
5/28/2022 5:17 AM
P
R
E Increment & Decrement Operators
P
A
R
E
D
Pre-Increment or Pre-Decrement (++a or --a)
CHANGE the value of the variable
B
Y USE the new value
S
U
L Post-Increment or Post-Decrement (a++ or a--)
A
V
USE the original value of the variable
CHANGE the value of the variable
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E EXAMPLE – FIVE
P
A /* Use of pre increment & post increment Operators */
R #include<stdio.h>
E
D #include<conio.h>
void main()
B {
Y
int a=20, b=10;
S printf("INCREMENT OPERATORS\n");
U printf("%d\n",a);
L printf("%d\n",++a);
A printf("%d\n",a++);
V printf("%d\n",a);
printf("\nDECREMENT OPERATORS\n");
N
E
printf("%d\n",b);
P printf("%d\n",--b);
A printf("%d\n",b--);
L printf("%d\n",b);
getch();
5/28/2022 5:17 AM
}
P
R
E Conditional Operator
P
A
R
The operator pair “?:” is known as conditional operator.
E
D
It takes three operands.
B
Y
Also called as ternary operator.
S
U
L
General form:
A 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛1? 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛2: 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛3
V 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛1 is evaluated first
N If 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛1 is true
E then value of 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛2 is the value of condition expression
P Else
A
L
the value of 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛3 is the value of conditional expression
5/28/2022 5:17 AM
P
R
E EXAMPLE – SIX
P
A
R
#include<stdio.h>
E
D
#include<conio.h>
B
void main()
Y { int num1, num2, larger;
S printf("Enter two numbers:\n");
U
L scanf("%d %d", &num1, &num2);
A
V larger=num1>num2?num1:num2;
N printf("\nThe larger number is %d", larger);
E
P getch();
A
L }
5/28/2022 5:17 AM
P
R
E Bitwise Operator
P
A
R Bitwise operators are used for manipulating data at bit level.
E
D
These operators are used for testing the bits or shifting them to the left or
B to the right.
Y
S
U Can be applied only to integer-type operands and not to float or double.
L
A
V Three types of bitwise operators
Bitwise Logical Operators
N
E Bitwise Shift Operators
P One’s Complement Operator
A
L
5/28/2022 5:17 AM
P
R
E Bitwise Logical Operator
P
A
R Performs logical tests between two integer-type operands.
E
D
5/28/2022 5:17 AM
P
R
E Bitwise AND (&)
P
A
R Logical ANDing between two operands.
E
D
The result of ANDing operation is 1 if both the bits have a value of
B
Y 1; otherwise it is 0.
S
U Consider 𝑛𝑢𝑚1 = 45 and 𝑛𝑢𝑚2 = 25
L
A 𝑛𝑢𝑚1 → 0000 0000 0010 1101
V
𝑛𝑢𝑚2 → 0000 0000 0001 1001
N
E
P If 𝑛𝑢𝑚3 = 𝑛𝑢𝑚1 & 𝑛𝑢𝑚2
A
L 𝑛𝑢𝑚3 → 0000 0000 0000 1001
5/28/2022 5:17 AM
P
R
E Bitwise OR (|)
P
A
R Logical ORing between two operands.
E
D
The result of ORing operation is 1 if either of the bits have a value of
B
Y 1; otherwise it is 0.
S
U Consider 𝑛𝑢𝑚1 = 45 and 𝑛𝑢𝑚2 = 25
L
A 𝑛𝑢𝑚1 → 0000 0000 0010 1101
V
𝑛𝑢𝑚2 → 0000 0000 0001 1001
N
E
P If 𝑛𝑢𝑚3 = 𝑛𝑢𝑚1 | 𝑛𝑢𝑚2
A
L 𝑛𝑢𝑚3 → 0000 0000 0011 1101
5/28/2022 5:17 AM
P
R
E Bitwise Exclusive XOR (^)
P
A
R Logical Exclusive ORing between two operands.
E
D
The result of Exclusive ORing operation is 1 only if one of the bits
B
Y have a value of 1; otherwise it is 0.
S
U Consider 𝑛𝑢𝑚1 = 45 and 𝑛𝑢𝑚2 = 25
L
A 𝑛𝑢𝑚1 → 0000 0000 0010 1101
V
𝑛𝑢𝑚2 → 0000 0000 0001 1001
N
E
P If 𝑛𝑢𝑚3 = 𝑛𝑢𝑚1 ^ 𝑛𝑢𝑚2
A
L 𝑛𝑢𝑚3 → 0000 0000 0011 0100
5/28/2022 5:17 AM
P
R
E EXAMPLE – SEVEN
P
A
R
#include<stdio.h>
E
D
void main()
B
{
Y int num1=45, num2=25,AND, OR, XOR;
S AND = num1 & num2;
U
L OR = num1 | num2;
A
V XOR = num1 ^ num2;
N printf("AND=%d\n",AND);
E
P printf("OR=%d\n", OR);
A
L printf("XOR=%d\n", XOR);
} 5/28/2022 5:17 AM
P
R
E Bitwise Shift Operators
P
A
R These operators are used to move bit patterns either to the left or to
E
D
the right.
B
Y There are two bitwise shift operators:
S Left shift (<<)
U
L Right shift (>>)
A
V
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E Bitwise Left Shift (<<) Operators
P
A
R It causes the operand to be shifted to the left by n positions.
E
D
operand<<n
B
Y The leftmost n bits in the original bit pattern will be lost and the
S rightmost n bits empty positions will be filled with 0’s.
U
L
A
V Example: 𝑛𝑢𝑚1 = 45; execute 𝑛𝑢𝑚2 = 𝑛𝑢𝑚1 ≪ 3;
5/28/2022 5:17 AM
P
R
E Bitwise Right Shift (>>) Operators
P
A
R It causes the operand to be shifted to the right by n positions.
E
D
operand>>n
B
Y The empty leftmost n bits position will be filled with 0’s, if the
S operand is an unsigned integer.
U
L
A
V Example: 𝑼𝒏𝒔𝒊𝒈𝒏𝒆𝒅 𝒊𝒏𝒕 𝒏𝒖𝒎𝟏 = 𝟒𝟓; execute 𝒏𝒖𝒎𝟐 = 𝒏𝒖𝒎𝟏 ≫ 𝟑;
N 𝒏𝒖𝒎𝟏 0000 0000 0010 1101
E Shift 1 0000 0000 0001 0110
P
A Shift 2 0000 0000 0000 1011
L Shift 3 0000 0000 0000 0101 𝑛𝑢𝑚2
5/28/2022 5:17 AM
P
R
E Bitwise One’s Complement Operator
P
A
R It is a unary operator which inverts all the bits represented by its
E
D
operand. i.e. all 0’s becomes 1’s and all 1’s becomes 0’s.
B
Y For any integer 𝒏, bitwise one’s complement of 𝒏 will be – ሺ𝒏 + 𝟏ሻ.
S
U
L Example: If 𝒏𝒖𝒎𝟏 = 𝟒𝟓, then we execute the statement; 𝒏𝒖𝒎𝟐 = ~𝒏𝒖𝒎𝟏;
A
V
5/28/2022 5:17 AM
P
R
E Bitwise One’s Complement Operator
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E Bitwise One’s Complement Operator
P
A
R
E
D
B
Y
S
U
L
A
V
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E EXAMPLE – EIGHT
P
A
R
#include<stdio.h>
E
D
void main()
B
{
Y int num1=45, left, right, comp;
S left = num1<<3;
U
L right = num1>>3;
A
V comp = ~num1;
N printf("%d\n", left);
E
P printf("%d\n", right);
A
L printf("%d\n", comp);
} 5/28/2022 5:17 AM
P
R
E Special Operators
P
A
R C supports some special operators like sizeof operator and comma
E
D
operator.
B
Y sizeof Operator:
S It is used with an operand to return the number of bytes it occupies.
U
L
A
V
The operand may be constant, variable or a data type qualifier.
N
E
P
A
L
5/28/2022 5:17 AM
P
R
E Special Operators
P
A
R
C supports some special operators like sizeof operator and comma
E operator.
D
B comma Operator:
Y It can be used to link related expressions together.
S
U A comma-linked list of expressions are evaluated from left-to-right and the
L value of the rightmost expression is the value of the combined expressions.
A
V
For example: 𝑛𝑢𝑚3 = ሺ𝑛𝑢𝑚1 = 45, 𝑛𝑢𝑚2 = 25, 𝑛𝑢𝑚1 +
N
𝑛𝑢𝑚2ሻ
E At first, 45 is assigned to 𝑛𝑢𝑚1
P Then, 25 is assigned to 𝑛𝑢𝑚2
A Then finally, sum of 𝑛𝑢𝑚1 and 𝑛𝑢𝑚2 is assigned to 𝑛𝑢𝑚3
L
5/28/2022 5:43 AM
P
R
E EXAMPLE – NINE
P
A
R
#include <stdio.h>
E
D
int main()
B
{
Y short int a = 0;
S printf("Size of variable a : %d\n",sizeof(a));
U
L
printf("Size of int data type : %d\n",sizeof(int));
A printf("Size of char data type : %d\n",sizeof(char));
V
printf("Size of float data type : %d\n",sizeof(float));
N
E printf("Size of double data type : %d\n",sizeof(double));
P return 0;
A
L }
5/28/2022 5:17 AM
P
R
E Expressions
P
A Expressions are the representation of code inside C text editor which is recognized by
R compiler.
E
D
An expression in C consists of syntactically valid combination of operators and operands
B that computes to a value.
Y
5/28/2022 5:17 AM
P
R
E Evaluation of Expressions
P
A
R
The changing an entity of one data type into another is called type casting or
E coercion.
D
5/28/2022 5:17 AM
P
R
E Implicit Type Casting
P
A
The conversion of data is performed either during compilation or run time is
R
E
called implicit type casting.
D
It is the automatic type conversion process performed by compiler itself.
B
Y
The data can be lost during this type of casting.
S
U
L The conversion from 𝑓𝑙𝑜𝑎𝑡 to 𝑖𝑛𝑡 can cause loss of higher order bits.
A
V
For example:
N int p, t, r, i;
E i=p*t*r/100.00;
P
A
Here, the variables are declared as integer but in the calculation 𝑖 = 𝑝 ∗ 𝑡 ∗
L
𝑟/100.00, after dividing by 100.00, the value of 𝑖 will change in to float variable.
5/28/2022 5:17 AM
P
R
E Explicit Type Casting
P
A
Explicit type conversion can either performed by built-in functions or by a special
R
E
syntax generated by coder.
D
These syntax changes one data type to other by using conversion keyword.
B
Y
It is a secure manner of changing variables from one data type to other.
S
U
L For example:
A int a=97, b=65;
V
printf("%c %c", (char)a, (char)b);
N
E Here, the ASCII value of 𝑎 and 𝐴 are 97 and 65 respectively. So, we print the
P value after type casting from 𝑖𝑛𝑡𝑒𝑔𝑒𝑟 to 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟.
A
L
5/28/2022 5:17 AM
P
R
E Operator Precedence and Associativity
P
A
R The precedence is used to determine how an expression involving
E
D
more than one operator is evaluated.
B
Y There are distinct level of precedence.
S
U
L The operators at the higher level of precedence are evaluated first.
A
V
5/28/2022 5:17 AM
Category Operator Associativity
P
R Postfix () [] -> . ++ -- Left to Right
E Multiplicative */% Left to Right
P
A Additive +- Left to Right
R Shift << >> Left to Right
E
D Relational < <= > >= Left to Right
Equality == != Left to Right
B
Y Bitwise AND & Left to Right
Bitwise OR | Left to Right
S
U Bitwise XOR ^ Left to Right
L Logical AND && Left to Right
A
V Logical OR || Left to Right
Comma , Left to Right
N
E Unary + - ! ~ ++ -- (type) * & sizeof Right to Left
P Conditional ?: Right to Left
A
L = += -= *= /= %= >>=
Assignment Right to Left
<<= &= ^= |=
5/28/2022 5:17 AM
P
R
E
P
A
R
E
D
B
Y
S
END OF UNIT FOUR
U
L
A
V
N
E
P
A
L
5/28/2022 5:14 AM