Operators in C
Subject: Data Structures and Algorithms · Language: C · Level: beginner · 4 min read
Practice arithmetic, relational, logical, assignment, increment, decrement, and conditional operators.
Operators are symbols that tell the compiler to perform an operation. In simple words, operators help us calculate, compare, assign, and make decisions using values and variables.
For example, in `a + b`, the `+` operator adds two values. In `a > b`, the `>` operator compares two values.
Why Operators Are Important
Almost every C program uses operators. When you calculate marks, check pass or fail, compare two numbers, update a counter, or choose the larger value, you are using operators.
Operands and Operators
The values on which an operator works are called operands.
In `10 + 3`, `10` and `3` are operands, and `+` is the operator.
Types of Operators in C
C provides several types of operators. A beginner should first understand what each group is used for.
Unary, Binary, and Ternary Operators
- Unary operators work on one operand, such as `++a` or `!flag`
- Binary operators work on two operands, such as `a + b`
- The conditional operator `?:` is ternary because it uses three parts
Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
- `+` adds two values
- `-` subtracts one value from another
- `*` multiplies values
- `/` divides values
- `%` gives the remainder after division
Example: `10 % 3` gives `1` because 3 goes into 10 three times and 1 is left.
Relational Operators
Relational operators compare two values. The result is either true or false. In C, true is usually represented by `1`, and false is represented by `0`.
- `>` checks greater than
- `<` checks less than
- `>=` checks greater than or equal to
- `<=` checks less than or equal to
- `==` checks equality
- `!=` checks not equal
Logical Operators
Logical operators are used to combine conditions.
- `&&` means AND. It is true only when both conditions are true
- `||` means OR. It is true when at least one condition is true
- `!` means NOT. It reverses the result
Example: `a > 0 && b > 0` checks whether both numbers are positive.
Bitwise Operators
Bitwise operators work on individual bits of integer values. They are useful in low-level programming, flags, masks, and some DSA problems.
- `&` performs bitwise AND
- `|` performs bitwise OR
- `^` performs bitwise XOR
- `~` performs bitwise NOT
- `<<` shifts bits left
- `>>` shifts bits right
Beginner students do not need to master bitwise operators immediately, but they should know that C supports them.
Assignment Operators
Assignment operators store or update values in variables.
- `=` assigns a value
- `+=` adds and assigns
- `-=` subtracts and assigns
- `*=` multiplies and assigns
- `/=` divides and assigns
- `%=` takes remainder and assigns
Example: `sum += 5;` means `sum = sum + 5;`.
Increment and Decrement Operators
These operators are used to increase or decrease a value by 1.
- `++` increases the value by 1
- `--` decreases the value by 1
They are commonly used in loops, counters, and index-based problems.
Conditional Operator
The conditional operator is a short form of `if else`. It uses three parts: condition, value when true, and value when false.
```c
max = (a > b) ? a : b;
```
This means: if `a` is greater than `b`, store `a` in `max`; otherwise store `b`.
```c
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
if (b != 0) {
printf("Quotient = %d\n", a / b);
printf("Remainder = %d\n", a % b);
} else {
printf("Division by zero is not allowed\n");
}
int greater = (a > b) ? a : b;
printf("Greater number = %d\n", greater);
return 0;
}
```
Important Points to Remember
- Integer division gives an integer result. For example, `10 / 3` gives `3`, not `3.33`
- The `%` operator gives the remainder and is mainly used with integers
- Use `==` for comparison and `=` for assignment
- Use parentheses when an expression becomes difficult to read
Operator Precedence
Operator precedence decides which operator runs first when an expression has many operators. For example, multiplication has higher precedence than addition.
```c
int result = 10 + 3 * 2;
```
Here `3 * 2` runs first, so the result becomes `16`, not `26`.
Use brackets when an expression is confusing. Brackets make the intention clear.
Associativity
When two operators have the same precedence, associativity decides the direction of evaluation. For most arithmetic operators, evaluation goes from left to right.
Still, for beginner code, prefer simple expressions and brackets. Readability is more important than writing clever one-line expressions.
Expression Evaluation
An expression is a combination of values, variables, and operators that produces a result.
```c
int total = marks1 + marks2 + bonus;
```
When solving coding questions, identify the expressions first. Most calculation problems are built from small expressions.
Practice Question
Write a C program that takes two integers as input and prints their sum, difference, product, quotient, remainder, and the greater number.
Practice Note
The runnable solution is given below. Change the input values and run it again. Try `20 5`, `15 4`, and `8 0` to understand normal division and division by zero handling.