Unit4 Operators and Expressions
Unit4 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: (+ , – , * , /)
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.
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 ~.
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are
opposite. It is denoted by ^.
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 >>.
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 . . .