Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

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).

Types of Arithmetic Operators in C


The C Arithmetic Operators are of two types based on the number of operands they work. These are as follows:
1. Binary Arithmetic Operators
2. Unary Arithmetic Operators
1. Binary Arithmetic Operators in C
The C binary arithmetic operators operate or work on two operands.
C provides 5 binary arithmetic operators for performing arithmetic functions which are as follows:
Name of
Operator Arithmetic Operation Syntax
Operator

+ Addition Add two operands. x+y

– Subtraction Subtract the second operand from the first operand. x–y

* Multiplication Multiply two operands. x*y

/ Division Divide the first operand by the second operand. x/y

% Modulus Calculate the remainder when the first operand is divided by the second operand. x%y

// C program to demonstrate syntax of binary arithmetic operators


#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;
}

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:

Operator Symbol Operation Implementation

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 Plus Operator + Returns the value of its operand. +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;

printf("Post Increment and Decrement\n");

// 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

printf("\nPre Increment and Decrement\n");

// pre-increment example:
// res is assigned 11 now since a is updated here itself

res = ++a;

// a and res have same values = 11


printf("a is %d and result is %d\n", a, res);

// pre-decrement example:
// res is assigned 10 only since a is updated here itself

res = --a;

// a and res have same values = 10

printf("a is %d and result is %d\n", a, res);

return 0;
}
Output
Post Increment and Decrement
a is 11 and result is 10
a is 10 and result is 11

Pre Increment and Decrement


a is 11 and result is 11
a is 10 and result is 10
Example 1: C Program to find the area of a rectangle and triangle.

#include <stdio.h>
int main()
{
// declaring dimensions of the rectangle
int length = 10;
int breadth = 5;

// declaring variables to store the results


int area, perimeter;

// 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 −

Operator Description Example

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 AND. True only if all operands are true

|| Logical OR. True only if either one operand is true

! Logical NOT. True only if the operand is 0

// 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);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n", result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n", result);

result = !(a != b);


printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", 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

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

~ Bitwise complement

<< Shift left

>> Shift right

//Bitwise Operator

#include <stdio.h>

int main()
{
int a = 12, b = 25;
clrscr();

printf("Output = %d\n", a & b);


printf("Output = %d\n", a | b);
printf("Output = %d", a ^ b);

getch();
return 0;
}

Output
Output = 8
Output = 29
Output = 21
Shift Operators

There are two shift operators in C programming:


 Right shift operator
 Left shift operator.

Right Shift Operator


Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted by >>.

Left Shift Operator


Left shift operator shifts all bits towards left by a certain number of specified bits. The bit positions that have been vacated
by the left shift operator are filled with 0. The symbol of the left shift operator is <<.

// 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.

Syntax of a conditional operator

Expression1? expression2: expression3;

Meaning of the above syntax


o In the above syntax, the expression1 is a Boolean condition that can be either true or false value.
o If the expression1 results into a true value, then the expression2 will execute.
o If the expression1 returns false value then the expression3 will execute.

// 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.

// Finding greatest of 3 numbers using Conditional Operator


#include<stdio.h>
void main()
{
int a, b, c, big ;
clrscr();
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? ( a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
getch();
}
The sizeof operator
The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
// Sizeof Operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}

Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

You might also like