Skip to main content

Operators in C Programming

What are Operators in C

Operators are special symbols that perform specific operations on one, two, or more operands, and then return a result.

For example:

  • The addition operator + performs addition on two operands and the assignment operator = assigns a value to a variable.

  • An operand is a variable, constant or expression that is operated upon by an operator.

C has a rich set of operators, including arithmetic operators, relational operators, logical operators, bitwise operators, and more.

Types of operators in C

Types of operators in C include:

  • Arithmetic Operators
  • Relational/Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Ternary Operator
  • Bitwise Operators
  • Special Operators
  • Increment and Decrement Operators

Table of Operators in C

Below is a table of common operators in C, grouped by type:

Type of OperatorOperatorExampleResult
Arithmetic+5 + 1015
-10 - 55
*5 * 1575
/15 / 53
%7 % 31
Comparison==5 == 5true
!=5 != 10true
>5 > 10false
<5 < 10true
>=5 >= 5true
<=5 <= 10true
Logical&&(5 > 2) && (10 < 20)true
||(5 > 2) || (10 < 5)true
!!(5 > 10)true
Assignment=x = 5x is 5
+=x += 5x is 10
-=x -= 5x is 5
*=x *= 5x is 25
/=x /= 5x is 5
Increment/Decrement++x++x is 6
--x--x is 5
Bitwise&x & ybitwise and of x and y
|x | ybitwise or of x and y
^x ^ ybitwise xor of x and y
~~xbitwise complement of x
<<x << ybitwise left shift of x by y
>>x >> ybitwise right shift of x by y
Specialsizeofsizeof(x)size of x in bytes
&&xaddress of x
**xvalue at the address x
.x.ymember y of struct x
->x->ymember y of struct x
Ternary?:x>y? x: yif x>y then x else y
note

This table is not an exhaustive list of all the operators in C, but rather a selection of the most commonly used operators.

Basic Example of operators in c

#include <stdio.h>

int main() {
int x = 5, y = 10, z = 15;

// Arithmetic operators
int sum = x + y; // sum = 15
int difference = y - x; // difference = 5
int multiplication = x * z; // multiplication = 75
int quotient = z / x; // quotient = 3

// Comparison operators
if (x < y) { // true
printf("x is less than y\n");
}
if (y >= z) { // false
printf("y is greater than or equal to z\n");
}

// Logical operator
if (x > 2 && y < 20) { // true
printf("x is greater than 2 and y is less than 20\n");
}

// Assignment operator
x = y; // x is now equal to 10

// Increment and Decrement operator
x++; // x is now equal to 11
y--; // y is now equal to 9
return 0;
}
Output:

Output:

x is less than y
x is greater than 2 and y is less than 20