0% found this document useful (0 votes)
9 views25 pages

Unit4 Operators and Expressions

The document provides an overview of operators and expressions in programming, detailing various types of operators including arithmetic, relational, logical, assignment, bitwise, and miscellaneous operators. It explains how these operators function with examples and sample programs demonstrating their usage. Additionally, it covers operator precedence and associativity, which determine the order of operations in expressions.

Uploaded by

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

Unit4 Operators and Expressions

The document provides an overview of operators and expressions in programming, detailing various types of operators including arithmetic, relational, logical, assignment, bitwise, and miscellaneous operators. It explains how these operators function with examples and sample programs demonstrating their usage. Additionally, it covers operator precedence and associativity, which determine the order of operations in expressions.

Uploaded by

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

Operators and Expressions

Prepared by
Smita Mande
Introduction to operators
▪ Operators are the special symbols which are used to indicate the
type of operation.
▪ Operands are variables or data member which are used in
conjunction with operators to evaluate the expression.
▪ Combination of operands and operators forms an expression
Arithmetic Operators
Arithmetic operator are of two types:
1. Unary Operators: Operators that operates or works with a single
operand are unary operators. For example: (++ ,- -)
2. Binary Operators: Operators that operates or works with two
operands are binary operators. For example: (+ , – , * , /,%)
Unary Operators
• Operators that operates or works with a single operand are unary operators.
For example: (++ ,- –)
1. Increment operator
It is used to increment the value of the variable by 1.
The increment can be done in two ways:

❖ prefix increment
In this method, the operator precedes the operand (e.g., ++a). The value of operand will be
altered before it is used.
int a = 1;
int b = ++a; // b = 2
❖ postfix increment
In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it
is used.
int a = 1;
int b = a++; // b = 1
int c = a; // c = 2
Sample program with output
int a, b, c = 0;
a = ++c;//a=1
b = c++; //b=1,c=2
printf(“%d %d %d”, a, b, ++c);
Output: /* 1 1 3 is printed */
Explanation:
c is incremented making its value 1.
The result assigned to a making its value 1.
The value of c is assigned to b making its value 1.
Then c is incremented making its value 2.
Finally, c is incremented before it is printed, making its value 3.
2. decrement
It is used to decrement the value of the variable by 1.
The decrement can be done in two ways:

❖ prefix decrement
In this method, the operator precedes the operand (e.g., – -a). The value of
operand will be altered before it is used.
int a = 1;
int b = --a; // b = 0

❖ postfix decrement
In this method, the operator follows the operand (e.g., a- -). The value of operand
will be altered after it is used.
int a = 1;
int b = a--; // b = 1
int c = a; // c = 0
Binary Operators
Operators that operates or works with two operands are binary
operators. For example: (+ , – , * , /)

Note: Both operand should be integer while using % operator.


Sample Program
//Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c; addition = 13
c = a+b; a-b = 5
printf(“addition of a and b= %d \n", c); a*b = 36
c = a-b;
a/b = 2
printf("a-b = %d \n", c);
c = a*b; Remainder when a divided
printf("a*b = %d \n", c); by b=1
c = a/b;
printf("a/b = %d \n", c);
c = a%b;
printf("Remainder when a divided by b = %d \n", c);
return 0;
}
Relational Operators
Relational operators are used to compare two quantities and take certain decision
depending on their relations
▪ if the specified relation is true then result is one
▪ if the specified relation is false then result is zero
Sample Program
// Working of relational operators
#include <stdio.h>
int main()
{ 5 == 5 is 1
int a = 5, b = 5, c = 10; 5 == 10 is 0
printf("%d == %d is %d \n", a, b, a == b); 5 > 5 is 0
printf("%d == %d is %d \n", a, c, a == c); 5 > 10 is 0
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c); 5 < 5 is 0
printf("%d < %d is %d \n", a, b, a < b); 5 < 10 is 1
printf("%d < %d is %d \n", a, c, a < c); 5 != 5 is 0
printf("%d != %d is %d \n", a, b, a != b); 5 != 10 is 1
printf("%d != %d is %d \n", a, c, a != c); 5 >= 5 is 1
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
5 >= 10 is 0
printf("%d <= %d is %d \n", a, b, a <= b); 5 <= 5 is 1
printf("%d <= %d is %d \n", a, c, a <= c); 5 <= 10 is 1
return 0;
}
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false.
Logical operators are commonly used in decision making in C programming.

Operator Meaning Example

If c = 5 and d = 2 then,
Logical AND. True only if
&& expression ((c==5) &&
all operands are true
(d>5)) equals to 0.

If c = 5 and d = 2 then,
Logical OR. True only if
|| expression ((c==5) || (d>5))
either one operand is true
equals to 1.

If x=6 , y = 5 then,
! Logical NOT. expression !(x>y) equals to
0
Sample Program
// Working of logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result); (a == b) && (c > b) is 1
result = (a == b) && (c < b); (a == b) && (c < b) is 0
printf("(a == b) && (c < b) is %d \n", result); (a == b) || (c < b) is 1
result = (a == b) || (c < b); (a != b) || (c < b) is 0
printf("(a == b) || (c < b) is %d \n", result);
!(a != b) is 1
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result); !(a == b) is 0
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Assignment operators
An assignment operator is used for assigning a value to a variable
Sum=5;
Operator Example Same as
A=sum;//a=5;
= a=b a=b
Sum=sum+marks1;//0+65
+= a += b a = a+b
Sum=sum+marks2;//65+25
-= a -= b a = a-b
Sum+=marks1;
*= a *= b a = a*b
Sum+=marks2;
/= a /= b a = a/b

%= a %= b a = a%b
Sample program
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf(“value of c = %d\n", c);
c += a;//c=c+a;
printf(" value of c = %d\n", c);
c -= a; // c is 5
printf(" value of c = %d\n", c);
c *= a; // c is 25
printf(" value of c = %d\n", c);
c /= a; // c is 5
printf(" value of c = %d\n", c);
c %= a; // c = 0
printf(" value of c = %d\n", c);
return 0;
}
Bitwise Operators
• It is used to perform bit-level operations on the operands. The operators are first
converted to bit-level and then the calculation is performed on the operands.

• Used in applications which require manipulation of individual bits within a word


of memory
Note: bitwise operations can be performed only on integer and character type data.
Bitwise AND operator &
The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit
of an operand is 0, the result of corresponding bit is evaluated to 0.
Let us suppose the bitwise AND operation of two integers 12 and 25.

12 = 00001100 (In Binary) #include <stdio.h>


25 = 00011001 (In Binary) int main()
Bit Operation of 12 and 25 {
int a = 12,b = 25;
00001100
printf("Output = %d", a & b);
& 00011001
return 0;
________
}
00001000 = 8 (In decimal)
Output = 8
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.

12 = 00001100 (In Binary) #include <stdio.h>


25 = 00011001 (In Binary) int main()
Bitwise OR Operation of 12 and 25 {
00001100 int a = 12, b = 25;
| 00011001 printf("Output = %d", a | b);
________ return 0;
00011101 = 29 (In decimal) }

Output = 29
Bitwise complement (NOT) operator ~

Bitwise compliment operator is an unary operator (works on only one operand). It changes
1 to 0 and 0 to 1. It is denoted by ~.

35 = 00100011 (In Binary)


Bitwise complement Operation of 35 ~
00100011
________
11011100 = 220 (In decimal)
Bitwise XOR (exclusive OR) operator ^

The result of bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.

12 = 00001100 (In Binary) #include <stdio.h>


25 = 00011001 (In Binary) int main()
Bitwise XOR Operation of 12 and 25 {
00001100 int a = 12, b = 25;
^ 00011001 printf("Output = %d", a^b);
________
return 0;
00010101 = 21 (In decimal)
}

Output = 21
Right Shift Operator
Right shift operator shifts all bits towards right by certain number of specified bits.
The bit positions that have been vacated by the right shift operator are filled with 0
It is denoted by >>.

212 = 11010100 (In binary)


212>>2 = 00110101 (In binary) [Right shift by two bits]
212>>7 = 00000001 (In binary)
212>>8 = 00000000
212>>0 = 11010100 (No Shift)

Left Shift Operator


Left shift operator shifts all bits towards left by a certain number of specified bits.
The bit positions that have been vacated by the left shift operator are filled with 0.
The symbol of the left shift operator is <<.

212 = 11010100 (In binary)


212<<1 = 10101000 (In binary) [Left shift by one bit]
212<<0 = 11010100 (No Shift)

212<<4 = 01000000 (In binary)


Miscellaneous Operators
Conditional Operator:
The conditional operator is similar to the if-else statement as it does follow the same
algorithm as of if-else statement but the conditional operator takes less space and
helps to write the if-else statements in the shortest way possible.

Syntax:
The conditional operator is of the form
variable = condition ? Expression1 : Expression2

Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also
called ternary operators.
Sizeof Operator
It is a compile-time unary operator and used to compute the size of its operand.
It returns the size of a variable.
It can be applied to any data type, float type, pointer type variables.
When sizeof() is used with the data types, it simply returns the amount of memory
allocated to that data type.
The output can be different on different machines like a 32-bit system can show
different output while a 64-bit system can show different of same data types.

int main()
{ // output
int a = 16;
Size of variable a : 4
printf("Size of variable a : %d\n",sizeof(a));
printf("Size of int data type : %d\n",sizeof(int)); Size of int data type : 4
printf("Size of char data type : %d\n",sizeof(char)); Size of char data type : 1
printf("Size of float data type : %d\n",sizeof(float)); Size of float data type : 4
printf("Size of double data type : %d\n",sizeof(double));
return 0; Size of double data type : 8
}
Operator Precedence determines which operator is performed first in an expression with
more than one operators with different precedence.
Operators Associativity is used when two operators of same precedence appear in an
expression. Associativity can be either Left to Right or Right to Left.
Precedence and associativity
Sample Code
#include<stdio.h>
int main()
{
int a,b;
printf("Enter values of a and b:");
scanf("%d%d", &a, &b);
printf("\n%d\t%d\t%d\t%d",++a, a, a++, a++);
printf("\n%d\t%d\t%d",b--,++b, b);
return 0;
}
Enter values of a and b:
8
6

11 10 9 8
7 7 6
--------------------------------
Process exited after 9.963 seconds with return value 0
Press any key to continue . . .

You might also like