What is Operators in C Programming Language?

Operators in c are defined as some symbols that carry out a specific mathematical/ logical computation on the given operands. So you can say that operators are one of the most important components of any programming language, and without them, programming languages are of no major use.

Let us understand what operators are by taking an example.

X = Y*Z Here, * is the multiplication operator that operates on the given operands Y and Z. 

There are various types of operators in c that are used to perform different types of functions. The operators supported by C are listed below.

Also Read: Recursion in C

1) What is Arithmetic Operators in C?

Arithmetic operators are used when mathematical operations have to be performed on the operands. These include, + (addition), – (subtraction), * (multiplication), / (division), % (modulus), ++ (increment), and – (decrement).

1. Addition (+) Operator in C

Addition Operators work by adding two operands and then storing the value in a variable. Mathematically explaining, the addition operator works like – 2 + 3 = 5

Also Read: What is the use of Operator Precedence & Associativity?

In programming, two variables are taken: a =2 and b = 3. Let us create another variable, c, which will store the resultant value. So, c = a + b. On using the print command to print c, 5 will be printed on the screen.

For instance,

#include<stdio.h>

int main()

{

int a = 2, b= 3, c;

c = a + b;

printf (“Result is %d”, c);

return 0;

}

Output

Result is 5

2. Subtraction Operator (-)

The subtraction Operator works by subtracting the second operand from the first operand. The second operand is the one that is present on the right-hand side of the ‘-’ operator. Mathematically, the subtraction operator works like – 3 – 2 = 1

Also Read: How to copy one string into another with or without using Strcpy Function in C?

In programming, two variables are taken: a = 3 and b = 2. Let us create another variable, c, which will store the resultant value. So, c = a – b. On using the printf command to print c, 1 will be printed on the screen.

For instance,

#include<stdio.h>

int main()

{

int a = 3, b = 2, c;

c=a-b;

printf(“Result is %d”, c);

return 0;

}

Output

Result is 1

3. Division Operator (/) 

Division Operator works by dividing the first operand by the second operand. Mathematically, a division operator works like – 10 / 2 = 5

In programming, two variables are taken: a = 10, b = 2. We will take another variable called c, which will store the resultant value. So, c = a / b. On using the printf command to print c, 5 will be printed.

Also Read: What is the difference between Strcpy & Strncpy in C?

For instance, 

#include<stdio.h>

int main()

{

int a = 10, b = 2, c;

c = a/b;

printf(“Result is %d”, c);

return 0;

}

Output

Result is 5

4. Multiplication Operator (*)

Multiplication Operator works by finding the product of two operands. Mathematically, the multiplication operator works like – 2 * 3 = 6

Also Read: What is Internshala?

In programming, two variables are taken: a = 2, b = 3. We will take another variable called c, which will store the resultant value. So, c = a * b. On using the printf command to print c, 6 will be printed.

For instance, 

#include<stdio.h>

int main()

{

int a = 2, b = 3, c;

c = a*b;

printf(“Result is %d”, c);

return 0;

}

Output

Result is 6

5. Modulus Operator (%)

When you divide two operands and want the remainder, use the modulus operator. Mathematically, 5 % 3 returns 2 as the remainder of this division is 2.

In programming, two variables are taken: a = 5, b = 3. We will take another variable called c, which will store the resultant value. So, c = a % b. On using the printf command to print c, 2 will be printed.

For instance, 

#include<stdio.h>

int main()

{

int a = 5, b = 3, c;

c = a%b;

printf(“Result is %d”, c);

return 0;

}

Output

Result is 2

2) What is a Unary Operator in C Programming?

These operators in C only need a single operand to perform operations. Unary Operators include ++ and –. These are called the increment and decrement operators in C, respectively.

1. Increment Operator in C

As the name suggests, the increment operator is used to increase the value of a given variable by 1. This means if we use an increment operator on 5, the result will be 6. The increment operator is of two types – Post Increment and Pre Increment operator.

Post increment operator works by increasing the value first and then printing it, while the pre-increment operator first increases the value and then prints it. Post-increment is represented as x++, and pre-increment as ++x.

Let us understand this better by looking at a piece of code.

Sample Code of Increment Operator in C 

#include<stdio.h>

int main()

{

int a=5, b, c;

b = a++; //Post-increment

printf(“b is %d\n”, b);

c = ++a; //Pre-increment

printf(“c is %d”, c);

return 0;

}

Output

b is 5

c is 7

Explanation

When we used a++, the value of a was incremented from 5 to 6 and stored in b, but since it is post-increment, 5 is printed on the screen.

When we used ++a, the value of a is incremented from 6 to 7 and stored in c, then printed as it is on the screen.

2. Decrement Operator

As the name suggests, a decrement operator is used to decrease the value of a given variable by 1. This means if we use a decrement operator on 5, the result will be 4. The decrement operator is of two types – Post Decrement and Pre Decrement.

Post decrement operator works by decreasing the value first and then printing it, while pre decrement operator first decreases the value and then prints it. Post-decrement is represented as x– and pre-decrement as –x.

Let us understand this better by looking at a piece of code.

Sample Code of Decrement Operator

#include<stdio.h>

int main()

{

int a=5, b, c;

b = a–; //Post-decrement operator

printf(“b is %d\n”, b);

c = –a; //Pre-decrement operator

printf(“c is %d”, c);

return 0;

}

Output

b is 5

c is 3

Explanation

When we used a–, the value of a was decremented from 5 to 4 and stored in b, but since it is post-decrement, 5 is printed on the screen.

When we use –a, the value of a is decremented from 4 to 3 and, stored in c, and then printed as it is on the screen.

3) What is Relational Operators in C Programming Language?

Relational Operators are used to compare the given operands. This includes checking whether the given operands are equal to each other, etc. Relational operators supported by C are listed below:

Operator Symbol Operator Meaning
==Equals to
>Greater than
>=Greater than or equals to
<Lesser than
<=Lesser than or equals to
!=Not equals to

If the given condition between two operands is true, then 1 is printed. In case the condition is false, then 0 is printed. Let us take an example to understand this better.

Sample Code of Relational Operator

#include<stdio.h>

int main()

{

int a=5, b=6, c=7;

printf(“%d == %d is %d \n”, a, b, a == b);

printf(“%d <= %d is %d \n”, a, c, a <= b);

}

Output

5==6 is 0

5<=7 is 1

4) What is a Logical Operator in C?

Logical Operators in C result in a boolean value which is either true or false.

Operator Symbol Operator Meaning
&&Logical AND
||Logical OR
!Logical NOT

 These operators (AND, OR, and NOT) are used to check the two or more than two different conditions.

1. (AND) && Operator in C

If anyone’s condition is false, then the output is false.

Also Read: What are C tokens?

For instance,

#include <stdio.h>

int main()

{

int d=6, ans;

ans = ((d>=10)&& (d==6));

printf (“AND operator output is %d\n,” ans);

return 0;

}

Output

AND operator output is 0

2. OR Operator in C (||)

If anyone’s condition is true, then the output is true; otherwise, it is false.

For instance,

#include <stdio.h>

int main()

{

int d=6, ans;

ans = ((d>=10)|| (d==6));

printf (“OR operator output is %d\n,” ans);

return 0;

}

Output

OR operator output is 1

3. Not Operator (!)

This operator inverts the output. It inverts ‘0’ to ‘1’ or ‘1’ to ‘0’.

For instance,

#include <stdio.h>

int main()

{

int d=6, ans;

ans = !((d>=10)|| (d==6));

printf (“NOT operator output is %d\n,” ans);

return 0;

}

Output

Not operator output is 0.

5) What do you mean by Bitwise Operator in C?

Whenever computations take place, the operators are converted into forms of bit-level. This is done to make the calculation easier and faster. Bitwise operators in C are used to perform these operations on the bit level.

Operator SymbolsOperator Meaning
&AND
|OR
^Exclusive OR
~Complement
<<To Shift Left (Left Shift Operator)
>>To Shift Right (Right Shift Operator)

1. Bitwise (AND) & Operator in C

AND Operator performs the operation on a bit level.

ABA & B
000
010
100
111

From the above table, if any bit is false(0), then the output will be false (0).

For instance,

#include <stdio.h>

int main()

{

int d=9, f = 3, ans; //In binary form 9 is 00001001 and 3 is 00000011

ans = d&f; //Bitwise AND operation

printf (“Bitwise AND operator output is %d\n,” ans);

return 0;

}

Output

Bitwise AND operator output is 1

2. Bitwise OR Operator (|)

When both conditions are true, then the output will be true.

ABA | B
000
010
100
111

For instance,

#include <stdio.h>

int main()

{

int d=9, f = 3, ans; //In binary form 9 is 00001001 and 3 is 00000011

ans = d|f; //Bitwise OR operation

printf (“Bitwise OR operator output is %d\n,” ans);

return 0;

}

Output

Bitwise OR operator output is 11

3. Bitwise XOR Operator (^)

The resultant output is 1 if both the inputs have two different values.

ABA’B+AB’
000
011
101
110

For instance,

#include <stdio.h>

int main()

{

int d=9, f = 3, ans; //In binary form 9 is 00001001 and 3 is 00000011

ans = d^f; //Bitwise XOR operation

printf (“Bitwise XOR operator output is %d\n,” ans);

return 0;

}

Output

Bitwise, the XOR operator output is 10

  1. Bitwise Complement Operator (~)

This operator inverts the bit. It turns 0 to 1 or 1 to 0.

For instance, 

#include <stdio.h>

int main()

{

int d=9, ans; //9 in binary form 00001001

ans = ~d;

printf (“Bitwise complement operator output %d\n,” ans);

return 0;

}

Output

Bitwise complement operator output is -10.

2. Bitwise Left Shift Operator in C

It shifts the bits towards your left-hand side.

For instance,

d = 9 <<1

<<1 means it shifts bits towards your left-hand side by one bit.

The binary form of 9 = 0 0 0 0 1 0 0 1

Left-shift = 0 0 0 0 1 0 0 1 << 1

Result = 0 0 0 1 0 0 1 0

For instance, 

#include <stdio.h>

int main()

{

int d=9, ans; //9 in binary form 00001001

ans = d<<1;

printf (“Bitwise left-shift operator output %d\n,” ans);

return 0;

}

Output

00010010

3. Bitwise Right Shift Operator

This Operator Shifts the bits towards your right-hand side.

Suppose, d = 9

d >> 1

Binary form of 9 = 0 0 0 0 1 0 0 1

0 0 0 0 1 0 0 1 >> 1

Result = 0 0 0 0 0 1 0 0

For instance,

#include <stdio.h>

int main()

{

int d=9, ans; //9 in binary form 00001001

ans = d>>1;

printf (“Bitwise right-shift operator output %d\n,” ans);

return 0;

}

Output

0000 0100

Some Points to remember

1.d = 9, d<<2, it means bits are 2-places shifted.

2. d=9

d<<31

It doesn’t mean it shifts the 31 bits towards the left. They need 32 bits for shifting 31 bits.

6) What are Assignment Operators in C Programming Language?

Assignment Operators are used for the assignment of values to variables. Variables are written on the left-hand side, while the operand on the right-hand side is the value that needs to be assigned. Example: x = 4, where x is a variable, 4 is a value, and = is the assignment operator.

Assignment Operator SymbolsWritten asEquivalent to
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
%=X%=yx=x%y

Sample Code for Assignment Operators:

#include <stdio.h>

int main()

{

int x=12, y=1;

y+=x;

printf (“The new value of y is %d\n,” y);

return 0;

}

Output

The new value of y is 13

7) Other Operators in C

In addition to the operators discussed above, there are some more operators that can be used to carry out some specific operations. These operators are discussed below:

Operator Name Operator Description
Sizeof operatorSizeof operator in C is a unary operator that can be used at the time of compilation to calculate the size of the operand. The result is of the type unsigned int.
Comma operatorThe comma operator is a binary operator in C.
Conditional operatorA conditional operator takes three expressions and forms a condition. It is written as – expression1? Expression2: Expression3.

 

If the condition on expression1 is true then expression 2 is evaluated and its results are returned. If the condition on expression1 turns out to be false than expression3 is evaluated and its result is returned. It is also known as the ternary operator.

Sample Code: sizeof operator in C

#include <stdio.h>

int main()

{

printf(“The size of integer variable is : %lu\n”, sizeof(int));

return 0;

}

Output

The size of the integer variable is: 4

Sample Code: Comma operator

#include <stdio.h>

int main()

{

int a=5, b=10;

printf(“The evaluated answer is : %d”, (a,b));

return 0;

}

Output

The evaluated answer is: 10

Sample Code for Conditional Operator or Ternary Operator in C

#include <stdio.h>

int main()

{

int a=10, b=20, min;

min=(a<b) ? a : b;

printf(“The smallest number among the two given numbers %d and %d is: %d\n “,a, b, min);

return 0;

Hello, My Name is Abhinav. I am an Author in the Education Category of Trickyedu. I have Done My Engineering in Computer Science from DIT University. I have a good command on Science, Programming Language, and microprocessors. So, I choose this platform to share my knowledge and experience.

Leave a Comment