Professional Documents
Culture Documents
Operators in C
Operators in C
An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic,
logical, bitwise, etc.
In other words, we can say that an operator operates the operands. For example, ‘+’ is an operator used for addition, as
shown below:
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 has many built-in operators and can be classified into 6 types:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
Arithmetic Operators
Arithmetic Operators are the operators which are used to perform mathematical calculations like addition (+), subtraction (-
), multiplication (*), division (/), and modulus (%). It performs all the operations on numerical values (constants and
variables).
– Subtraction Subtract the second operand from the first operand. x–y
% Modulus Calculate the remainder when the first operand is divided by the second operand. x%y
// 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;
}
Output
a is 10 and b is 4
a + b is 14
a - b is 6
a * b is 40
a / b is 2
a % b is 2
2. Unary Arithmetic Operators in C
The unary arithmetic operators operate or work with a single operand. In C, we have two unary arithmetic operators
which are as follows:
Decrement Operator -- Decreases the integer value of the variable by one. --h or h--
Increment Operator ++ Increases the integer value of the variable by one. ++h or h++
Unary Minus Operator – Returns the negative of the value of its operand. -h
Increment Operator in C
The ‘++’ operator is used to increment the value of an integer. It can be used in two ways:
1. Pre-Increment
When placed before the variable name (also called the pre-increment operator), its value is incremented instantly.
Consider the example:
a = ++x;
This example can be expanded to
a = (x = x + 1);
2. Post Increment
When it is placed after the variable name (also called post-increment operator), its value is preserved temporarily until
the execution of this statement and it gets updated before the execution of the next statement. For example:
a = x++;
It can be expanded to
a = x;
x = x + 1;
Decrement Operator in C
The ‘–‘ operator is used to decrement the value of an integer. Just like the increment operator, the decrement operator
can also be used in two ways:
1. Pre-Decrement
When placed before the variable name (also called the pre-decrement operator), its value is decremented instantly. For
example, – – x.
2. Post Decrement
When it is placed after the variable name (also called post-decrement operator), its value is preserved temporarily until
the execution of this statement and it gets updated before the execution of the next statement. For example, x – –.
Example of Unary Operators in C
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// post-increment example:
// res is assigned 10 only, a is not updated yet
res = a++;
printf("a is %d and result is %d\n", a, res); // a becomes 11 now
// post-decrement example:
// res is assigned 11 only, a is not updated yet
res = a--;
printf("a is %d and result is %d\n", a, res); // a becomes 10 now
// pre-increment example:
// res is assigned 11 now since a is updated here itself
res = ++a;
// pre-decrement example:
// res is assigned 10 only since a is updated here itself
res = --a;
return 0;
}
Output
Post Increment and Decrement
a is 11 and result is 10
a is 10 and result is 11
#include <stdio.h>
int main()
{
// declaring dimensions of the rectangle
int length = 10;
int breadth = 5;
// calculating area
area = length * breadth;
// calculating perimeter
perimeter = 2 * (length + breadth);
// printing results
printf("Area = %d\nPerimeter = %d", area, perimeter);
return 0;
}
Output
Area = 50
Perimeter = 30
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
The following table shows all the relational operators supported by C language. Assume variable A holds 10 and
variable B holds 20 then −
Checks if the values of two operands are equal or not. If yes, then the condition
== (A == B) is not true.
becomes true.
Checks if the values of two operands are equal or not. If the values are not equal,
!= (A != B) is true.
then the condition becomes true.
Checks if the value of left operand is greater than the value of right operand. If yes,
> (A > B) is not true.
then the condition becomes true.
Checks if the value of left operand is less than the value of right operand. If yes,
< (A < B) is true.
then the condition becomes true.
Checks if the value of left operand is greater than or equal to the value of right
>= (A >= B) is not true.
operand. If yes, then the condition becomes true.
Checks if the value of left operand is less than or equal to the value of right
<= (A <= B) is true.
operand. If yes, then the condition becomes true.
// Relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
Operator Meaning
// Logical operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
C Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-
level which makes processing faster
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
//Bitwise Operator
#include <stdio.h>
int main()
{
int a = 12, b = 25;
clrscr();
getch();
return 0;
}
Output
Output = 8
Output = 29
Output = 21
Shift Operators
// Shift Operators
#include <stdio.h>
int main()
{
int num=212;
printf("Right shift by %d: %d\n", num >> 1);
printf("\n");
printf("Left shift by %d: %d\n", num << 1);
return 0;
}
Output
Right Shift by 1: 106
Left Shift by 1: 424
Conditional Operator
The conditional operator is also known as a ternary operator. The conditional statements are the decision-making
statements which depend upon the output of the expression. It is represented by two symbols, i.e., '?' and ':'.
As conditional operator works on three operands, so it is also known as the ternary operator.
// Conditional Operator
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for voting"));
return 0;
}
In the above code, we are taking input as the 'age' of the user. After taking input, we have applied the condition by using a
conditional operator. In this condition, we are checking the age of the user. If the age of the user is greater than or equal to
18, then the statement1 will execute, i.e., (printf("eligible for voting")) otherwise, statement2 will execute, i.e., (printf("not
eligible for voting")).
// Conditional Operator
#include <stdio.h>
int main()
{
int a=5,b; // variable declaration
b=((a==5)?(3):(2)); // conditional operator
printf("The value of 'b' variable is : %d",b);
return 0;
}
In the above code, we have declared two variables, i.e., 'a' and 'b', and assign 5 value to the 'a' variable. After the
declaration, we are assigning value to the 'b' variable by using the conditional operator. If the value of 'a' is equal to 5 then
'b' is assigned with a 3 value otherwise 2.
return 0;
}
Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte