BCA Sem 1 | Unit 1 | Computer Concepts & C Programming
Operators in C Language — BKNMU Junagadh
Arithmetic, Relational, Logical, Bitwise, Assignment, Increment/Decrement & Special Operators
a + b, + is the operator, while a and b are operands).
C operators can be categorized into three groups based on how many operands they operate upon:
1. Unary Operators
Operates on a single operand. Examples: Increment (++), Decrement (--), Logical NOT (!), Address-of (&).
2. Binary Operators
Operates on two operands. Includes Arithmetic (+, -, *, /, %), Relational, and Logical operators.
3. Ternary Operator
Operates on three operands. C has exactly one ternary operator: Conditional Operator (?:).
The 8 Major Operator Types
Here is a detailed breakdown of all 8 functional operator categories in C:
| Category | Operators | Description | Example Expression |
|---|---|---|---|
Arithmetic | +, -, *, /, % | Performs basic mathematical calculations (Note: % gives remainder) | 10 % 3 → 1 |
Relational | <, >, <=, >=, ==, != | Compares two values; returns 1 (True) or 0 (False) | 5 > 3 → 1 |
Logical | && (AND), || (OR), ! (NOT) | Combines conditional expressions | (x > 0 && y < 10) |
Assignment | =, +=, -=, *=, /=, %= | Assigns right-hand value to left-hand variable | x += 5 (same as x = x + 5) |
Increment / Decrement | ++, -- | Increases or decreases variable value by 1 (Prefix & Postfix) | a++ or ++a |
Bitwise | &, |, ^, ~, <<, >> | Performs bit-level operations on binary bits | 5 & 3 (Bitwise AND) |
Conditional | ? : | Shorthand for if-else condition evaluation | max = (a > b) ? a : b; |
Special | sizeof, &, *, , | Returns memory size, address, pointer dereference, or separates expressions | sizeof(int) → 2/4 Bytes |
% calculates the remainder of integer division. It can ONLY be used with integer operands (e.g., 5.5 % 2 is a compile error!).
- Rule: "Change first, then use."
- Increments value of
xby 1 first - Uses updated value in current expression
- Example: If
x=5,y = ++x;→x=6, y=6
- Rule: "Use first, then change."
- Uses current value of
xin expression first - Increments
xby 1 afterwards - Example: If
x=5,y = x++;→x=6, y=5
Observe how different operators evaluate expressions in a working C program:
* before +). Operator Associativity determines direction (Left-to-Right or Right-to-Left) when operators have equal precedence!
Highest Priority: Brackets & Unary
(), [], Unary ++, --, !, sizeof (Associativity: Right-to-Left for Unary).
Arithmetic Priority
Multiplicative *, /, % evaluated before Additive +, - (Associativity: Left-to-Right).
Relational & Logical Priority
Relational <, > evaluated before Logical && and ||.
Lowest Priority: Assignment & Comma
Assignment operators (=, +=) and comma , are evaluated last (Associativity: Right-to-Left for Assignment).
= (assigns value) with Equal To Relational Operator == (compares equality)! Writing if (x = 5) will assign 5 to x and evaluate as True!
- Operator & Operand: Operator specifies action; Operand is data being operated on.
- 3 Classifications: Unary (1 operand), Binary (2 operands), Ternary (3 operands).
- Modulo Operator (%): Returns remainder of integer division; invalid for floating-point values.
- Logical Operators:
&&(AND - both true),||(OR - at least one true),!(NOT - flips result). - Ternary Operator:
condition ? expr1 : expr2;— shorthand forif-else. - Short-Circuit Evaluation: In
&&, if first condition is false, second condition is not evaluated. - sizeof Operator: Returns size of data type/variable in bytes.