Skip to content

C Programming - Operators

C operators are symbols that perform operations on variables and values. They are essential for performing arithmetic, logical, comparative, and many other operations.

C operators are categorized in the following categories −

Operator Types

  • Arithmetic Operators: +, -, *, /, %
  • Relational Operators: ==, !=, >, < , >=, < =
  • Logical Operators: &&, ||, !
  • Bitwise Operators: &, |, ^, ~, < < , >>
  • Assignment Operators: =, +=, -=, *=, /=, %=
  • Increment/Decrement Operators: ++, —

In Programming Languages, the associativity of an operator is a property that determines how operators of same precedence are grouped together in the absence of parenthesis.

OperatorAssociativityDescription
++, --Right to LeftIncrement, Decrement
*, /, %Left to RightMultiplication, Division
+, -Left to RightAddition, Subtraction
==, !=Left to RightEquality, Inequality
&&Left to RightLogical AND
=Right to LeftAssignment

The assignment operator is used to assign values to variables.

OperatorExampleMeaning
=x = yAssign y to x
+=x += yx = x + y
-=x -= yx = x - y
*=x *= yx = x * y
/=x /= yx = x / y
%=x %= yx = x % y
&=x &= yx = x & y
^=x ^= yx = x ^ y
<<=x <<= yx = x << y
>>=x >>= yx = x >> y

Operate on individual bits of data.

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (NOT)
  • << (left shift)
  • >> (right shift)
  • Logical AND (&&):
    • Returns true if both operands are true.
  • Logical OR (||):
    • Returns true if at least one of the operands is true.
  • Logical NOT (!):
    • Returns true if the operand is false.
#include <stdio.h>
int main() {
int a = 10, b = 5, result;
int x = 1, y = 0;
// Arithmetic Operators
result = a + b;
printf("Addition: %d + %d = %d\n", a, b, result);
result = a - b;
printf("Subtraction: %d - %d = %d\n", a, b, result);
result = a * b;
printf("Multiplication: %d * %d = %d\n", a, b, result);
result = a / b;
printf("Division: %d / %d = %d\n", a, b, result);
result = a % b;
printf("Modulus: %d %% %d = %d\n", a, b, result);
// Relational Operators
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a < b);
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
// Logical Operators
printf("x && y: %d\n", x && y); // AND
printf("x || y: %d\n", x || y); // OR
printf("!x: %d\n", !x); // NOT
// Assignment Operators
result = a; // Assignment
result += 5; // result = result + 5
printf("Result after += 5: %d\n", result);
result *= 2; // result = result * 2
printf("Result after *= 2: %d\n", result);
return 0;
}