Module2 Chapter1
Module2 Chapter1
a + b Expression
a _ b
Operators
i) Unary operator: An operator which acts on only one operand to produce the result is
called Unary operator.
Ex: -10, -a, *b, ++a, a++, b-- etc.
ii) Binary operator: An operator which acts on two operands to produce the result is called
Binary operator.
Ex: a+b, a*b, 10/5 etc
iii) Ternary operator: An operator which acts on three operands to produce the result is
called Ternary operator.
Ex: a ? b : c;
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 1
2. Classification of operators based on type of operation
Arithmetic Operators Ex: +, - , * etc.
i) Arithmetic Operators
The operators that are used to perform arithmetic operations such as addition,
subtraction, multiplication, division and modulus are called arithmetic operators.
These operators perform operations on two operands and hence they are called binary
operators.
% (modulus operator) divides the first operand by second and returns the remainder.
% (modulus operator) cannot be applied to floating or double.
% operator returns remaining value(remainder) of an integer division.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 2
Arithmetic Operator’s Precedence (Precedence of operators)
The Arithmetic Expressions are evaluated based on “BODMAS” Rule. (Brackets of Operator
(x(2),[ ]), Order or Power (xy,23 etc.), Division, Multiplication, Addition, Subtraction).
variable= expression;
Ex: a=10;
a=b;
a=a+b;
Area= l*b; //Result of Expression l*b is copied into variable area.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 3
c). Multiple Assignment Statement
Assigning a value or a set of values to different variables in one statement is called multiple
assignment statement.
The multiple assignments are used whenever same value has to be copied into various memory
locations.
Ex: int i=10; //simple assignment
int j=10;
int k=10;
int i=j=k=10; //multiple assignment
1. Post Increment
It increments the value after (post) the operand value is used. i.e., operand value is used
first and then the operand value is incremented by 1.
Ex: void main()
{
int a=20,b; // a=20, b?
b=a++; // b=a=20, a=a+1= 20+1
printf(“%d”,a); // a=21
printf(“%d”,b); // b=20
}
2. Pre Increment
It increments before (pre) the operand value is used. The operand value is incremented by
1 and this incremented value is used.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 4
Ex: void main()
{
int a=20,b; // a=20, b?
b=++a; // a=++a=a+1= 20+1, b=a=21
printf(“%d”,a); // a=21
printf(“%d”,b); // b=21
}
Decrement Operator
'--' is a decrement operator. This is a unary operator. It decrements the value of a variable
by one.
Post Decrement Ex: a--
Decrement
Operator Pre Decrement Ex: --a
1. Post Decrement
It decrements the value after (post) the operand value is used. i.e., operand value is used
first and then the operand value is decremented by 1.
Ex: void main()
{
int a=20,b; // a=20, b?
b=a--; // b=a=20, a=a-1= 20-1
printf(“%d”,a); // a=19
printf(“%d”,b); // b=20
}
2. Pre Decrement
It decrements before (pre) the operand value is used. The operand value is decremented by
1 and this decremented value is used.
Ex: void main()
{
int a=20,b; // a=20, b?
b=--a; // a=--a=a-1= 20-1, b=a=19
printf(“%d”,a); // a=19
printf(“%d”,b); // b=19
}
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 5
Relational operators available in C are:
All the relational operators are having a same priority and left to right associativity.
The relational operators have lower precedence than arithmetic operators.
Equality operators
C supports two kinds of equality operators to compare their operands for strict equality or
inequality.
equal ==
not equal ! =
Equality operators have lower precedence than the relational operators.
v) Logical operators
The operators that are used to combine two or more relational expressions are called
logical operators.
The output of relational expression is true or false, the output of logical expression is also
true or false.
Logical NOT: The logical NOT operator is denoted by ‘!’. The output of not operator can be true or
false. The result is true if the operand value is false and the result is false if the operand is true.
Logical AND: The logical AND operator is denoted by ‘&&’. The output of and operator is true if
both the operands are evaluated to true. If one of the operand is evaluated false, the result is false.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 6
Logical OR: The logical OR operator is denoted by ‘||’. The output of or operator is true if and only
if at least one of the operands is evaluated to true. If both the operands are evaluated to false, the
result is false.
Ex: 1. If a, b, c are 3 sides of a triangle, then if a==b && b==c && c==a then triangle is equilateral
otherwise not an equilateral triangle.
2. If a, b, c are 3 sides of a triangle then if a==b || b==c || c==a then triangle is isosceles triangle
otherwise not a isosceles triangle.
3. int a=10, b:
b =!a ;
Output: value of b=0, because a = 10 then !a=0 and !a value assigned to b.
Where,
- expr1 is evaluated first.
- If expr1 is evaluated to true, then expr2is evaluated.
- If expr1 is evaluated to false, then expr3is evaluated.
Example:
1. Write a C program to find the biggest of two numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b,big;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
big=(a>b)?a:b;
printf(“Big=%d”,big);
}
2. Write a C program to find the smallest of two numbers using conditional operator.
#include<stdio.h>
void main()
{
int a,b,small;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
small=(a<b)?a:b;
printf(“Small=%d”,big);
}
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 7
vii) Bitwise Operators
The operators that are used to manipulate the bits of given data are called bitwise
operators.
These may only be applied to integral operand. i.e., char, short, int and long whether signed or
unsigned.
a. One’s Complement(~)
The operator that is used to change every bit from 0 to 1 and 1 to 0 in the specified operand
is called One’s complement operator.
Truth table of One’s Complement(~):
Op1 ~Op1
0 1
1 0
Output: ~10=245
Binary Representation:
10 = 0 0 0 0 1 0 1 0
245= 1 1 1 1 0 1 0 1
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 8
b. Left shift operator (<<)
The operator that is used to shift the data by a specified number of bit positions towards
left is called ‘left shift operator’.
Syntax:
b=a<<num;
Ex: Write a C program to show the usage of Left Shift operator.
#include<stdio.h>
void main()
{
int a=5,b;
b=a<<1;
printf(“%d<<1=%d”,a,b);
}
Output: 5<<1=10
MSB is discarded
0 0 0 0 0 1 0 1 a=5
b=10
0 0 0 0 1 0 1 0
0 is appended at LSB
0 0 0 0 1 0 1 0 a=10
0 0 0 0 0 1 0 1 b=5
0 is appended at MSB
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 9
d. Bit-wise AND (&)
If the corresponding bit positions in both the operands are 1, then AND operation results in
1, otherwise AND operation results in 0.
Truth table of Bit-wise AND (&):
Op1 Op2 Op1&Op2
0 0 0
0 1 0
1 0 0
1 1 1
Ex: Write a C program to show the usage of ‘&’ operator.
#include<stdio.h>
void main()
{
int a=10,b=6;
c=a&b;
printf(“%d&%d=%d”,a,b,c);
}
Output: 10&6=2
Binary Representation:
10 = 0 0 0 0 1 0 1 0
06 = 0 0 0 0 0 1 1 0
02 = 0 0 0 0 0 0 1 0
e. Bit-wise OR (|)
If the corresponding bit positions in both the operands are 0, then OR operation results in
0, otherwise OR operation results in 1.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 10
f. Bit-wise XOR (^)
If the corresponding bit positions in both the operands are different, then XOR operation
results in 1, otherwise XOR operation results in 0.
0^0=0
0^1=1
1^0=1
1^1=0
Ex: Write a C program to show the usage of ‘^’ operator.
#include<stdio.h>
void main()
{
int a=10,b=6;
c=a^b;
printf(“%d^%d=%d”,a,b,c);
}
Output: 10^6=12
Binary Representation:
10 = 0 0 0 0 1 0 1 0
06 = 0 0 0 0 0 1 1 0
12 = 0 0 0 0 1 1 0 0
1. Comma operator
Comma Operator has the least precedence among all the operators and it is left associative
operator.
Comma Operator is used in the declaration to separate the variables.
Ex: int a,b,c;
It can be used to separate the items in the list.
Ex: a=12,345,678;
It can be used to combine two or more statements into a single statement.
Ex: sum=a+b,sub=a-b,mul=a*b,div=a/b,mod=a%b;
2. sizeof()
‘sizeof()’ operator is used to determine the number of bytes occupied by a variable or a constant
in the memory.
Ex: sizeof(char) 1 byte
sizeof(int) 2 bytes
sizeof(float) 4 bytes
Example program: Write a C program that computes the size of int, float, char and double
variables.
#include<stdio.h>
void main()
{
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 11
char ch;
int x;
float y;
double z;
clrscr();
printf(“Number of bytes occupied by character variable=%d”,sizeof(ch));
printf(“Number of bytes occupied by integer variable=%d”,sizeof(x));
printf(“Number of bytes occupied by floating-point variable=%d”,sizeof(y));
printf(“Number of bytes occupied by double variable=%d”,sizeof(z));
}
𝑥 = √2𝜋𝑛 x=sqrt(2*3.142*n)
a a/b
b
𝑏 x=-b/(2*a)
𝑥=−
2𝑎
2
𝑎𝑥 + bx + c a*x*x+b*x+c
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 12
2.1.3 Associativity of operators
When two or more operators have the same precedence, then precedence rules are not
applicable.
Associativity determines how the operators with the same precedence are evaluated in an
expression.
The two types of operators Associativity are:
1. Left Associativity
2. Right Associativity
1. Left Associativity
In an expression, if two or more operators having the same priority are evaluated from
left-to-right, then the operators are called Left to Right associative operators.
We normally denote it using L R.
2. Right Associativity
In an expression, if two or more operators having the same priority are evaluated from
right-to-left, then the operators are called Left to Right associative operators.
We normally denote it using R L.
Ex: i=j=k=10.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 13
Unary +, - and * have higher precedence than the binary forms.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 14
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 15
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 16
2.3 Type Conversion and Typecasting
The process of converting the data or variable from one data type to another data type is
called Type Conversion or Typecasting.
Type conversion is done implicitly by the compiler, whereas typecasting has to be done
explicitly by the programmer.
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 17
C compiler converts the data type with lower rank to the data type with higher rank. This
process of conversion of data from lower rank to higher rank automatically by the C
compiler is called “Implicit type Conversion”.
If one operand type is same as that of other operand type, no conversion takes place.
Ex: int + int = int, float + float = float
If one operand type is ‘int’ and other operand type is ‘float’, then the operand with type int is
promoted to ‘float’ (because float is up in ladder compared with int).
Type conversion is automatically done when we assign an integer value to floating point
variable. Consider the code given below in which an integer data type is promoted to float. This
is known as promotion (where the lower level data type is promoted to higher type).
float x;
int y=3;
x=y;
Now, x=3.0, as automatically integer value is converted into its equivalent floating point
representation.
2.3.2 Typecasting
Typecasting is also known as forced conversion.
It is done when the value of a higher data type has to be converted into a value of a lower data
type.
But this casting is done under the programmer’s control and not under the compiler’s
control.
The programmer can instruct the compiler to change the type of the operand or variable
from one data type to another data type. This forcible conversion from one data type to
another data type is called “Explicit type Conversion” (Type Casting).
Syntax:
(type) Expression
Dr. Azizkhan F Pathan, Prof. Meghana G R, Prof. Kotramma T S, Dept. of CS&E, JIT, Davangere 18