Skip to main content

Operators in C

Operators are special symbols in C that perform operations on variables and values. Understanding operators is essential for writing effective C programs. This guide will cover the various types of operators available in C.

1. Arithmetic Operators​

Arithmetic operators are used to perform basic mathematical operations.

OperatorDescriptionExample
+Additionint sum = a + b;
-Subtractionint diff = a - b;
*Multiplicationint prod = a * b;
/Divisionint quot = a / b;
%Modulus (Remainder)int rem = a % b;

Example:​

int a = 10, b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int prod = a * b; // 30
int quot = a / b; // 3
int rem = a % b; // 1

2. Relational Operators​

Relational operators compare two values and return a boolean result (true or false).

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Example:​

#include <stdio.h>

int main() {
int x = 10, y = 5;
printf("%d\n", (x == y)); // Output: 0 (false)
printf("%d\n", (x != y)); // Output: 1 (true)
printf("%d\n", (x > y)); // Output: 1 (true)
printf("%d\n", (x < y)); // Output: 0 (false)
printf("%d\n", (x >= y)); // Output: 1 (true)
printf("%d\n", (x <= y)); // Output: 0 (false)
return 0;
}

3. Logical Operators​

Logical operators are used to perform logical operations and combine multiple conditions.

OperatorDescriptionExample
&&Logical AND(x > 5 && y < 10)
!Logical NOT!(x > 5)

Example:​

#include <stdio.h>

int main() {
int x = 10, y = 5;
printf("%d\n", (x > 5 && y < 10)); // Output: 1 (true)
printf("%d\n", (x > 5 || y > 10)); // Output: 1 (true)
printf("%d\n", !(x > 5)); // Output: 0 (false)
return 0;
}

4. Assignment Operators​

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns valuex = y
+=Adds and assignsx += y
-=Subtracts and assignsx -= y
*=Multiplies and assignsx *= y
/=Divides and assignsx /= y
%=Modulus and assignsx %= y

Example:​

#include <stdio.h>

int main() {
int x = 10, y = 5;
x += y; // Equivalent to x = x + y
printf("%d\n", x); // Output: 15
return 0;
}

5. Increment and Decrement Operators​

Increment and decrement operators are used to increase or decrease a variable's value by 1.

OperatorDescriptionExample
++Increments value++x or x++
--Decrements value--x or x--

Example:​

#include <stdio.h>

int main() {
int x = 10;
printf("%d\n", ++x); // Output: 11 (Pre-increment)
printf("%d\n", x--); // Output: 11 (Post-decrement)
printf("%d\n", x); // Output: 10
return 0;
}

6. Bitwise Operators​

Bitwise operators operate on bits and perform bit-level operations.

| Operator | Description | Example | | -------- | ----------- | ---------- | --- | --- | | & | Bitwise AND | x & y | | | | Bitwise OR | x | y | | ^ | Bitwise XOR | x ^ y | | ~ | Bitwise NOT | ~x | | >> | Left shift | x >> 2 | | << | Right shift | x << 2 |

Example:​

#include <stdio.h>

int main() {
int x = 5, y = 9;
printf("%d\n", (x & y)); // Output: 1
printf("%d\n", (x | y)); // Output: 13
return 0;
}

7. Ternary Operator​

The ternary operator is a shorthand for an if-else statement.

OperatorDescriptionExample
?:Ternarycondition ? expr1 : expr2

Example:​

#include <stdio.h>

int main() {
int x = 10;
int result = (x > 5) ? 100 : 200;
printf("%d\n", result); // Output: 100
return 0;
}


Understanding these operators is key to mastering C programming and writing efficient code!