FPL Notes Unit2
FPL Notes Unit2
For example,
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are
operands. The addition operator tells the compiler to add both of the operands ‘a’
and ‘b’.
Types of Operators in C
C language provides a wide range of operators that can be classified into 6 types
based on their functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
2.1 Arithmetic operators
Arithmetic operators are used to perform common mathematical
operations
Integer Arithmetic
When both the operands are integer.
a-b=10
a+b=18
Real Arithmetic
When both the operands are real.
x=6.0/7.0
y=1.0/3.0
Mixed Mode Arithmetic
When one operand is real and another is integer.
x=15/10.0
y=15/10
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// printing a and b
printf("a is %d and b is %d\n", a, b);
res = a + b; // addition
printf("a + b is %d\n", res);
res = a - b; // subtraction
printf("a - b is %d\n", res);
res = a * b; // multiplication
printf("a * b is %d\n", res);
res = a / b; // division
printf("a / b is %d\n", res);
res = a % b; // modulus
printf("a %% b is %d\n", res);
return 0;
}
#include <stdio.h>
int main()
int a = 10, b = 4;
if (a > b)
else
if (a >= b)
printf("a is greater than or equal to b\n");
else
if (a < b)
else
if (a <= b)
else
// equal to
if (a == b)
else
// not equal to
if (a != b)
else
return 0;
}
2.3 Logical operators
Logical operators in C are used to combine multiple conditions/constraints. Logical
Operators returns either 0 or 1, it depends on whether the expression result is true or false.
In C programming for decision-making, we use logical operators.
We have 3 logical operators in the C language:
Logical AND ( && )
Logical OR ( || )
Logical NOT ( ! )
If both operands are non-zero then the condition becomes true. Otherwise, the result has a
value of 0. The return type of the result is int. Below is the truth table for the logical AND
operator.
X Y X && Y
1 1 1
1 0 0
0 1 0
0 0 0
2. Logical OR Operator ( || )
X Y X || Y
1 1 1
1 0 1
0 1 1
0 0 0
Syntax
(operand_1 || operand_2)
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if (a > 0 || b > 0) {
printf("Any one of the given value is "
"greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}
Logical NOT Operator ( ! )
If the condition is true then the logical NOT operator will make it false and
vice-versa. Below is the truth table for the logical NOT operator.
X !X
0 1
1 0
Syntax
!(operand_1 && operand_2)
#include <stdio.h>
int main()
{
int a = -1, b = 20;
if (a > 0 || b > 0) {
printf("Any one of the given value is "
"greater than 0\n");
}
else {
printf("Both values are less than 0\n");
}
return 0;
}
2. ―+=‖: This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the
current value of the variable on left to the value on the right and then assigns the result to
the variable on the left. Example:
(a += b) can be written as (a = a + b)
3. ―-=‖ This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts
the value on the right from the current value of the variable on left and then assigns the
result to the variable on the left. Example:
(a -= b) can be written as (a = a - b)
4. ―*=‖ This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies
the current value of the variable on left to the value on the right and then assigns the result
to the variable on the left. Example:
(a *= b) can be written as (a = a * b)
5. ―/=‖ This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the
current value of the variable on left by the value on the right and then assigns the result to
the variable on the left. Example:
(a /= b) can be written as (a = a / b)
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a is %d\n", a);
a += 10;
printf("Value of a is %d\n", a);
a -= 10;
printf("Value of a is %d\n", a);
a *= 10;
printf("Value of a is %d\n", a);
a /= 10;
printf("Value of a is %d\n", a);
return 0;
}
2.5 Increment and Decrement operators
The increment ( ++ ) and decrement ( — ) operators in C are unary operators for
incrementing and decrementing the numeric values by 1 respectively.
Syntax of Increment Operator
Increment Operator can be used in two ways which are as follows:
// AS PREFIX
++m
// AS POSTFIX
m++
1.Pre-Increment
In pre-increment, the increment operator is used as the prefix. Also known as prefix
increment, the value is incremented first according to the precedence and then the less
priority operations are done.
Example
result = ++var1;
2.Post-Increment
In post-increment, the increment operator is used as the suffix of the operand. The
increment operation is performed after all the other operations are done. It is also known as
postfix increment.
Example
result = var1++;
Decrement Operator in C
The decrement operator is used to decrement the value of a variable in an expression. In the
Pre-Decrement, the value is first decremented and then used inside the expression.
Syntax
Just like the increment operator, the decrement operator can also be used in two ways:
// AS PREFIX
--m
// AS POSTFIX
m--
1.Pre-Decrement Operator
The pre-decrement operator decreases the value of the variable immediately when
encountered. It is also known as prefix decrement as the decrement operator is used as the
prefix of the operand.
Example
result = --m;
2.Post-Decrement Operator
The post-decrement happens when the decrement operator is used as the suffix of the
variable. In this case, the decrement operation is performed after all the other operators are
evaluated.
Example
result = m--;
#include<stdio.h>
int main()
{
int a = 5;
int b = 5;
int prefix = ++a;
printf("Prefix Increment: %d\n", prefix);
printf("Prefix Increment: %d\n", a);
int postfix = b++;
printf("Postfix Increment: %d \n", postfix);
printf("Postfix Increment: %d \n", b);
return 0;
}
#include<stdio.h>
int main()
{
int a = 5;
int b = 5;
int prefix = --a;
printf("Prefix Increment: %d\n", prefix);
printf("Prefix Increment: %d\n", a);
int postfix = b--;
printf("Postfix Increment: %d \n", postfix);
printf("Postfix Increment: %d \n", b);
return 0;
}
2.6 Conditional operators
It is also known as the ternary operator in C as it operates on three
operands.
variable = Expression1 ? Expression2 : Expression3;
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d",
m, n)
: printf("n is greater than m that is %d > %d",
n, m);
return 0;
}
2.7 Bitwise operators.
In C, the following 6 operators are bitwise operators (also known as bit operators as
they work at the bit-level).
&,|,<<,>>,~,^
1. The & (bitwise AND) in C takes two numbers as operands and does AND on every bit
of two numbers. The result of AND is 1 only if both bits are 1.
2. The | (bitwise OR) in C takes two numbers as operands and does OR on every bit of
two numbers. The result of OR is 1 if any of the two bits is 1.
3. The ^ (bitwise XOR) in C takes two numbers as operands and does XOR on every bit
of two numbers. The result of XOR is 1 if the two bits are different.
4. The << (left shift) in C takes two numbers, the left shifts the bits of the first operand,
and the second operand decides the number of places to shift.
5. The >> (right shift) in C takes two numbers, right shifts the bits of the first operand,
and the second operand decides the number of places to shift.
6. The ~ (bitwise NOT) in C takes one number and inverts all bits of it.
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
2.9. Special operators
Comma operator and sizeof operator.
The comma operator can be used to link the related expression together.
It has the lowest pripority
Value=(x=10,y=5,x+y);
Sizeof operator.
It is compile time operator and when used with an operand it returns the no of bytes the
operand occupies.
M=sizeof(sum);
N=sizeof(a);
#include <stdio.h>
int main()
{
int a;
float b;
char c;
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(b));
printf("%d\n", sizeof(c));
return 0;
}
2.10 Arithmetic expression
An Arithmetic Expression is a combination of operands and Arithmetic operators, such as
addition, subtraction, and so on.
These combinations of operands and operators should be mathematically meaningful,
otherwise, they can not be considered as an Arithmetic expression in C.
When an expression contains only integral operands, then it is known as pure integer
expression when it contains only real operands, it is known as pure real expression, and when
it contains both integral and real operands, it is known as mixed mode expression.
#include <stdio.h>
int main()
{
// Verifying the result of the same expression
printf("100 / 5 % 2 = %d", 100 / 5 % 2);
return 0;
}
2.13 Operator precedence and Associativity
In general, the concept of precedence and associativity is applied together in expressions.
So let’s consider an expression where we have operators with various precedence and
associativity
2.14 Mathematical Functions
There is also a list of math functions available, that allows you to perform mathematical tasks
on numbers.
To use them, you must include the math.h header file in your program:
#include <math.h>
square Root
Example
printf("%f", sqrt(16));
Round a Number
The ceil() function rounds a number upwards to its nearest integer, and the floor() method
rounds a number downwards to its nearest integer, and returns the result:
Example
printf("%f", ceil(1.4));
printf("%f", floor(1.4));
Power
Example
printf("%f", pow(4, 3));
#include <math.h>
#include <stdio.h>
int main()
{
printf("value1 = %f\n",sqrt(16));
printf("value2 = %f\n",pow(4,2));
printf("value3 = %f\n",ceil(12.8));
printf("value4 = %f\n",floor(12.4));
return (0); }