Use of C Operators Arithmetic, Relational, Logical
INTRODUCTION
An operator is a symbol that tells the compiler to perform specific mathematical
or logical functions. C language is rich in built-in operators and provides the
following types of operators −
Arithmetic Operators
Relational Operators
Logical Operators
Examples :
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer B%A=0
division.
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
Use of C Operators Arithmetic, Relational, Logical
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and
variables).
Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)
The operators + , - and * computes addition, subtraction, and multiplication
respectively as you might have expected.
In normal calculation, 9/4 = 2.25 . However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also
an integer. The compiler neglects the term after the decimal point and shows
answer 2 instead of 2.25 .
The modulo operator % computes the remainder. When a=9 is divided by b=4 ,
the remainder is 1 . The % operator can only be used with integers.
Suppose a = 5.0 , b = 2.0 , c = 5 and d = 2 . Then in C programming,
Use of C Operators Arithmetic, Relational, Logical
PROGRAM
Example of Arithmatic Operators
// Working of arithmetic operators
#include <stdio.h>
int main()
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Use of C Operators Arithmetic, Relational, Logical
Output
Relational Operators
Relational operators are used for comparison of two values to understand the
type of relationship a pair of number shares. For example, less than, greater than,
equal to etc. Let’s see them one by one
Equal to operator: Represented as ‘==’, the equal to operator checks whether the
two given operands are equal or not. If so, it returns true. Otherwise it returns
false. For example, 5==5 will return true.
Not equal to operator: Represented as ‘!=’, the not equal to operator checks
whether the two given operands are equal or not. If not, it returns true.
Otherwise it returns false. It is the exact boolean complement of the ‘==’
operator. For example, 5!=5 will return false.
Greater than operator: Represented as ‘>’, the greater than operator checks
whether the first operand is greater than the second operand or not. If so, it
returns true. Otherwise it returns false. For example, 6>5 will return true.
Use of C Operators Arithmetic, Relational, Logical
Less than operator: Represented as ‘<‘, the less than operator checks whether the
first operand is lesser than the second operand. If so, it returns true. Otherwise it
returns false. For example, 6<5 will return false.
Greater than or equal to operator: Represented as ‘>=’, the greater than or equal
to operator checks whether the first operand is greater than or equal to the
second operand. If so, it returns true else it returns false. For example, 5>=5 will
return true.
Less than or equal to operator: Represented as ‘<=’, the less than or equal
tooperator checks whether the first operand is less than or equal to the second
operand. If so, it returns true else false. For example, 5<=5 will also return true.
Example of Relational Operators
// C program to demonstrate working of relational operators
#include <stdio.h>
int main()
int a = 10, b = 4;
// greater than example
if (a > b)
printf("a is greater than b\n");
else
printf("a is less than or equal to b\n");
// greater than equal to
Use of C Operators Arithmetic, Relational, Logical
if (a >= b)
printf("a is greater than or equal to b\n");
else
printf("a is lesser than b\n");
// less than example
if (a < b)
printf("a is less than b\n");
else
printf("a is greater than or equal to b\n");
// lesser than equal to
if (a <= b)
printf("a is lesser than or equal to b\n");
else
printf("a is greater than b\n");
// equal to
if (a == b)
printf("a is equal to b\n");
else
printf("a and b are not equal\n");
// not equal to
if (a != b)
printf("a is not equal to b\n");
Use of C Operators Arithmetic, Relational, Logical
else
printf("a is equal b\n");
return 0;
Output
Logical Operators:
They are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition under consideration. They are described
below:
Logical AND operator: The ‘&&’ operator returns true when both the conditions
under consideration are satisfied. Otherwise it returns false. For example, a && b
returns true when both a and b are true (i.e. non-zero).
Logical OR operator: The ‘||’ operator returns true even if one (or both) of the
conditions under consideration is satisfied. Otherwise it returns false. For
example, a || b returns true if one of a or b or both are true (i.e. non-zero). Of
course, it returns true when both a and b are true.
Use of C Operators Arithmetic, Relational, Logical
Logical NOT operator: The ‘!’ operator returns true the condition in consideration
is not satisfied. Otherwise it returns false. For example, !a returns true if a is false,
i.e. when a=0.
Example of Logical Operators
// C program to demonstrate working of logical operators
#include <stdio.h>
int main()
int a = 10, b = 4, c = 10, d = 20;
// logical operators
// logical AND example
if (a > b && c == d)
printf("a is greater than b AND c is equal to d\n");
else
printf("AND condition not satisfied\n");
// logical OR example
if (a > b || c == d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal "
" to d\n");
// logical NOT example
Use of C Operators Arithmetic, Relational, Logical
if (!a)
printf("a is zero\n");
else
printf("a is not zero");
return 0;
Output
Use of C Operators Arithmetic, Relational, Logical