Operators in C Language

Operators are symbols, which guide the compiler to perform a specific task on given values. For example +,-,* are operators for the mathematical task. C language has a rich set of operators, we will discuss in detail here.

Types of Operators in C

C language has different types of Operators for different tasks.

  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator
  4. Bitwise Operator
  5. Special Operator

Unary Operators in C

A Unary operator in C is an operator with a single operand in an expression or statement. It increases (++) or decreases (‐‐) the value of a variable by one.

  • Prefix
  • Postfix

prefix – When Operator is before the variable, it is called prefix. First, it increases the value, then assigns it to a variable or statement.

Example 1: pre-increment or prefix example in C.

#include<stdio.h>
int main()
{
int a=10,b;
	b=++a;
	printf("\nB=%d",b);
	printf("\nA=%d",a);
return 0;
}
Output :
B=11
A=11

postfix – When the Operator is after the variable, it is called postfix. First, it assigns the value to a variable or statement, then it increases or decreases the value of a variable by 1.

Example 2: Post increment or postfix example in C.

#include<stdio.h>
int main()
{
int a=10,b;
	b=a++;
	printf("\nB=%d",b);
	printf("\nA=%d",a);
return 0;
}
Output :
B=10
A=11

Binary Operators in C

Binary operators require two operands in an expression. After execution it returns a result, you need to carry in other variables or statements.

  • Arithmetic Operator (Mathematical Operator)
  • Assignment Operator
  • Relational Operator (Comparison Operator)
  • Logical Operator

Arithmetic Operator (Mathematical Operator)

OperatorDescription
+For Addition
For Subtraction
*Multiplication
/Divison
%Remainder of Division (modulus)

Example 3: Arithmetic Operation Example in C.

#include<stdio.h>
int main()
{
int a=25,b=10,c=0;
	c=a+b;
	printf("\nSum=%d",c);
	c=a-b;
	printf("\nSubtract=%d",c);
	c=a*b;
	printf("\nMultiply=%d",c);
	c=a/b;
	printf("\nDivison=%d",c);
	c=a%b;
	printf("\nRemainder=%d",c);

return 0;
}
OutPut :
Sum=35
Subtract=15
Multiply=250
Divison=2
Remainder=5

Assignment Operator

Assignment Operator assigns values from right side operands to left side operand

OperatorExample
= a=5, 5 assign to variable a
+= a=a+b can also write as a+=b
-= a=a-b can also write as a-=b
*= a=a*b can also write as a*=b
/= a=a/b can also write as a/=b
%= a=a%b can also write as a%=b

Relational Operator (Comparison Operator)

The Relational operator checks the relation or compares two operands and returns Boolean value as a result.

OperatorDescription
< Less then
> Greater then
<= Less then or Equal
>= Greater then or equal
==Equal to
!=Not equal to

Example 4: Relational Operator example in C.

#include<stdio.h>
int main()
{
int a=25,b=10;

	printf("\nA < b  = %d",a<b);
	printf("\nA > b  = %d",a>b);
	printf("\nA <= b = %d",a<=b);
	printf("\nA >= b = %d",a>=b);
	printf("\nA == b = %d",a==b);
	printf("\nA != b = %d",a!=b);

return 0;
}
If True then result is 1
If False then result is 0
Ouput :
A < b = 0
A > b = 1
A <= b = 0
A >= b = 1
A == b = 0
A != b = 1

Logical Operators

Logical operators in C are used to combine relational expression and return boolean value as a result. Before we start, keep in mind that all non zero values are considered as true (1) and zero as false in C language. There are three types of logical operators,

Operator Name Description
&& Logical ANDReturn TRUE if all expression is true, otherwise FALSE.
|| Logical ORReturn TRUE if all or any one expression is true, otherwise FALSE.
! Logical NOTReverse the state of the expression(TRUE expression to False and FALSE to TRUE).

Example 5 : Logical operator example in C.

#include<stdio.h>
int main()
{
int n=5,ans;
	ans=n>0 && n<100;
	printf("\nResult = %d",ans);
	//Result is 1, because both expression is true
	ans=n>50 && n<100;
	printf("\nResult = %d",ans);
	//Result is 0, because one expression is false

	ans=n>0 || n<100;
	printf("\nResult = %d",ans);
	//Result is 1, because both expression is true
	ans=n>50 || n<100;
	printf("\nResult = %d",ans);
	//Result is 1, because at least one expression is true

	ans=!(n>0);
	printf("\nResult = %d",ans);
	//Result is 0, because not reverse the state of expression
	ans=!(n>50);
	printf("\nResult = %d",ans);
	//Result is 1, because not reverse the state of expression
return 0;
}
output :
Result : 1
Result = 0
Result = 1
Result = 1
Result = 0
Result = 1

Ternary Operator in C (Conditional Operator)

A Ternary operator in C also known as a conditional operator. It uses three operands, so, it is called ternary. It works based on three parts, condition, true and false.

Syntax:
Condition ? expression 1 : expression 2
  • ? is a ternary operator
  • expression 1 executed, when the condition is true.
  • expression 2 executed, when the condition is false.

Example 6: Conditional operator in C, to check number is Even or Odd.

#include<stdio.h>
int main()
{
	int n;
	printf("\nEnter a number");
	scanf("%d",&n);
	(n%2==0) ? printf("Even") : printf("Odd");
return 0;
}
Output :
Enter a number 23
Odd

Bitwise Operators in C

Bitwise operators in C are used to work with binary numbers. Decimal numbers are converted into binary first, then bitwise operator works on each bit.

Operators Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
<< Left Shift
>> Right Shift

Example 7: Bitwise Operator example in C.

#include<stdio.h>
int main()
{
	int a=11, b=3;
	printf("\nBitwise And : %d",a & b);
	// 00001011 & 00000011, result 00000011 ( 3 as Decimal) 
	printf("\nBitwise Or : %d",a | b);
	// 00001011 | 00000011, result 00001011 ( 11 as Decimal)
	printf("\nBitwise XOR : %d",a ^ b);
	// 00001011 ^ 00000011, result 00001000 ( 8 as Decimal)
	printf("\nBitwise Complement : %d",~a);
	// 2's Complement
	printf("\nLeft Shift : %d",a << b);
	// 00001011 << 3, result 01011000 ( 88 as Decimal)
	// remove 3 digit from left and add three 0 to right
	printf("\nRight Shift : %d",a >> b);
	// 00001011 >> 3, result 00000001 ( 1 as Decimal)
	// remove 3 digit from right and add three 0 to left

return 0;
}
Output :
Bitwise And : 3
Bitwise Or : 11
Bitwise XOR : 8
Bitwise Complement : -12
Left Shift : 88
Right Shift : 1

Special Operators in C Language

Some more operators in C, known as special operators.

Operators Description Example
sizeof Return sizeof a variable. printf(“\n%d”,sizeof(var));
& Address Operator. &var, return address of a variable
* Pointer *var, Pointer variable