Operators in C
Types of Operators in C
An operator is a symbol that tells the compiler to perform a specific operation on one or more operands. C has a rich set of operators that can be categorized as:
- Arithmetic Operators:
+-*/% - Relational (Comparison) Operators:
==!=><>=<= - Logical Operators:
&&||! - Assignment Operators:
=+=-=*=/=%= - Increment/Decrement:
++-- - Bitwise Operators:
&|^~<<>> - Ternary Operator:
? : - sizeof Operator:
sizeof()
1. Arithmetic Operators
Used to perform basic mathematical operations.
| Operator | Name | Example (a=10, b=3) | Result |
|---|---|---|---|
+ | Addition | a + b | 13 |
- | Subtraction | a - b | 7 |
* | Multiplication | a * b | 30 |
/ | Division | a / b | 3 (integer division!) |
% | Modulus (Remainder) | a % b | 1 |
#include <stdio.h> int main() { int a = 10, b = 3; 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); // Integer division printf("a %% b = %d\n", a % b); // %% prints single % return 0; }
10 / 3 gives 3, not 3.33! When both operands are integers, C performs integer division (truncates the decimal). Use 10.0 / 3 or cast: (float)10 / 3 to get 3.333333.
2. Relational Operators
Used to compare two values. Always return 1 (true) or 0 (false).
| Operator | Meaning | a=5, b=3 | Result |
|---|---|---|---|
== | Equal to | a == b | 0 (false) |
!= | Not equal to | a != b | 1 (true) |
> | Greater than | a > b | 1 (true) |
< | Less than | a < b | 0 (false) |
>= | Greater than or equal | a >= 5 | 1 (true) |
<= | Less than or equal | a <= 3 | 0 (false) |
= is assignment (sets a value). == is comparison (checks equality). Writing if (a = 5) instead of if (a == 5) is a classic bug!
3. Logical Operators
Used to combine multiple conditions. Return 1 (true) or 0 (false).
| Operator | Name | Example | Result |
|---|---|---|---|
&& | AND | 1 && 0 | 0 — both must be true |
|| | OR | 1 || 0 | 1 — at least one true |
! | NOT | !1 | 0 — reverses the value |
#include <stdio.h> int main() { int marks = 85, attendance = 78; // Eligible if marks >= 80 AND attendance >= 75 if (marks >= 80 && attendance >= 75) { printf("Eligible for scholarship\n"); } // Fail if marks < 40 OR attendance < 75 if (marks < 40 || attendance < 75) { printf("Not eligible\n"); } return 0; }
4. Assignment Operators
| Operator | Example | Equivalent to |
|---|---|---|
= | a = 5 | a = 5 |
+= | a += 3 | a = a + 3 |
-= | a -= 2 | a = a - 2 |
*= | a *= 4 | a = a * 4 |
/= | a /= 2 | a = a / 2 |
%= | a %= 3 | a = a % 3 |
5. Increment & Decrement Operators
++ increases a value by 1, -- decreases by 1. These can be pre (before variable) or post (after variable).
#include <stdio.h> int main() { int a = 5; printf("a++ = %d\n", a++); // Prints 5, THEN increments to 6 printf("a = %d\n", a); // Now a is 6 printf("++a = %d\n", ++a); // Increments to 7, THEN prints 7 printf("a-- = %d\n", a--); // Prints 7, THEN decrements to 6 printf("--a = %d\n", --a); // Decrements to 5, THEN prints 5 return 0; }
6. Ternary Operator ( ? : )
A short form of if-else. Syntax: condition ? value_if_true : value_if_false
#include <stdio.h> int main() { int marks = 75; char *result = (marks >= 40) ? "Pass" : "Fail"; printf("Result: %s\n", result); int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max: %d\n", max); return 0; }
Operator Precedence (Priority)
When an expression has multiple operators, precedence determines which gets evaluated first. Higher precedence = evaluated first.
| Priority | Operators | Associativity |
|---|---|---|
| 1 (Highest) | () [] . -> | Left to Right |
| 2 | ++ -- ! ~ (cast) sizeof | Right to Left |
| 3 | * / % | Left to Right |
| 4 | + - | Left to Right |
| 5 | << >> | Left to Right |
| 6 | < <= > >= | Left to Right |
| 7 | == != | Left to Right |
| 8–12 | Bitwise &, ^, |, &&, || | Left to Right |
| 13 | ?: | Right to Left |
| 14 (Lowest) | = += -= *= /= | Right to Left |
Example: 2 + 3 * 4 → 3 * 4 = 12 first, then 2 + 12 = 14 (not 20!).
Use parentheses () to override precedence: (2 + 3) * 4 = 20.
Summary
- Arithmetic:
+-*/%— basic math operations - Relational:
==!=><— compare values, return 0 or 1 - Logical:
&&||!— combine conditions - Assignment:
=+=-=— assign values - Increment/Decrement:
++--— increase/decrease by 1 - Ternary:
? :— one-line if-else - Don't confuse
=(assignment) with==(comparison)
Write a program that takes two numbers and prints: their sum, difference, product, quotient, remainder, which is greater (using ternary), and whether their sum is even or odd (using %).
Frequently Asked Questions
What are operators in C?
+ for addition or == for comparison. C groups them into arithmetic, relational, logical, assignment, bitwise and a few others, each for a different kind of task.What are the types of operators in C?
+ - * / %), relational (< > == !=), logical (&& || !), assignment (= += -=), bitwise (& | ^), and special ones like the increment ++, decrement -- and the ternary ?:.What is the difference between = and == in C?
= is the assignment operator, which stores a value in a variable. A double == is the equality operator, which compares two values and gives true or false. Mixing them up is a very common bug, especially inside if conditions.What is the modulus operator in C?
% gives the remainder of an integer division. For example 10 % 3 is 1, because 10 divided by 3 leaves a remainder of 1. It is often used to check even/odd numbers or to wrap values around a range.