2.6 OPERATORS AND EXPRESSIONS
Programming in C
OPERATORS AND EXPRESSIONS IN C
An operator is a symbol that specifies the mathematical, logical, relational, or bitwise operation to be performed on operands.
An expression is a combination of variables, constants, and operators that evaluates to a single value.
When an expression is evaluated, it may produce:
-
a final value, and
-
side effects, which are permanent changes in program state (such as assignment, increment, or decrement).
Thus, C expressions differ from mathematical expressions because they may cause side effects.
Classification of Operators in C
C operators are classified as shown in Fig. 8.16 into:
-
Arithmetic Operators
-
Assignment Operators
-
Relational Operators
-
Equality Operators
-
Logical Operators
-
Bitwise Operators
-
Conditional (Ternary) Operator
-
Comma Operator
-
Other Operators (sizeof, membership, indirection)
Types of Operators in C
1. Arithmetic Operators
-
Unary:
+,-,++,-- -
Binary:
+,-,*,/,% -
Ternary:
?:(conditional)
2. Assignment Operators
-
Simple Assignment:
= -
Compound Assignment:
+=,-=,*=,/=,%=,&=,^=,|=
3. Relational Operators
>, <, >=, <=
4. Equality Operators
==, !=
5. Logical Operators
&&, ||, !
6. Bitwise Operators
&, |, ~, ^, <<, >>
7. Others
-
,(comma) -
*(indirection) -
.and->(membership)
1 Arithmetic Operators in C
Arithmetic operators are used to perform numeric computations.
They are classified into binary, unary, and ternary operators.
1.1 Binary Arithmetic Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 12 + 4.9 | 16.9 |
| - | Subtraction | 3.98 – 4 | –0.02 |
| * | Multiplication | 2 * 3.4 | 6.8 |
| / | Division | 9 / 2.0 | 4.5 |
| % | Remainder | 13 % 3 | 1 |
Important Points
-
If both operands are integers, result is integer
-
If any operand is real, result is real
-
Integer division truncates the remainder
Example:
9/2 → 4, –9/2 → –4 -
%operator:-
Works only on integers
-
Returns remainder
-
Cannot be applied to
floatordouble
-
Division by Zero
-
Illegal operation
-
Results in run-time error
-
Behaviour is undefined
Overflow
Occurs when result exceeds storage capacity.
Outcome is machine dependent and undefined.
1.2 Unary Operators
Unary Minus (–)
-
Negates the operand
-
Does not modify original value
Increment and Decrement Operators
-
++ (increment)
-
-- (decrement)
Rules
-
Operand must be a variable
-
Cannot be applied to constants or expressions
1.3 Pre and Post Increment / Decrement
Prefix (++a / --a)
-
Value is changed first
-
Then used in expression
Postfix (a++ / a--)
-
Value is used first
-
Then modified
Example
Result: a = 6, b = 6
Result: a = 6, b = 5
Important Note
-
Order of evaluation of function arguments is compiler-dependent
-
Expressions like:
have undefined behaviour
2 Relational Operators in C
Relational operators are used to compare two numeric values.
They evaluate to:
-
1 → true
-
0 → false
Relational Operators
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal |
| < | Less than |
| <= | Less than or equal |
| > | Greater than |
| >= | Greater than or equal |
Important Points
-
Operands must be numeric
-
Characters are allowed (ASCII values)
-
Strings must not be compared using relational operators
-
Use
strcmp()instead
-
3 Logical Operators in C
Logical operators are used to form logical expressions.
Logical Operators
| Operator | Meaning |
|---|---|
| ! | Logical NOT |
| && | Logical AND |
| ** |
Truth Rules
-
Non-zero → true
-
Zero → false
Short-Circuit Evaluation
-
In
&&, if left operand is false, right operand is not evaluated -
In
||, if left operand is true, right operand is not evaluated
4 Bitwise Operators in C
Bitwise operators operate on individual bits of integer operands.
Bitwise Operators
| Operator | Meaning |
|---|---|
| ~ | Bitwise NOT |
| & | Bitwise AND |
| ** | ** |
| ^ | Bitwise XOR |
| << | Left shift |
| >> | Right shift |
Key Points
-
Operands must be integers
-
Bits shifted left/right by specified positions
-
Vacated bits are filled with 0
-
Generally used with unsigned types
5 Conditional (Ternary) Operator
The conditional operator has three operands.
Syntax
-
If
expression1is true,expression2is evaluated -
Else,
expression3is evaluated -
Only one expression is evaluated
Example
Equivalent to if–else construct.
6 Comma Operator
The comma operator allows multiple expressions to be evaluated from left to right.
-
Value of last expression is returned
-
Ensures sequence control
Example
Final value assigned to j is 6
Applications
-
Used in for loops
-
Ensures complete evaluation order
-
Acts as a sequence point
7 sizeof Operator
The sizeof operator returns the size (in bytes) of a data type or expression.
Characteristics
-
Compile-time operator
-
Machine dependent
-
Accepts type or expression
Example
8 Expression Evaluation – Precedence and Associativity
Precedence
-
Determines which operator is evaluated first
Associativity
-
Determines direction of evaluation when precedence is same
Key Points
-
Unary operators > Binary operators > Ternary operator
-
Assignment has low precedence
-
Comma operator has lowest precedence
-
Parentheses override precedence
Example
Evaluation:
-
*first →5 – 14 – 9 -
Left-to-right
– -
Final result: –18
Conclusion
Operators and expressions form the core of computation in C.
Understanding operator types, side effects, precedence, and associativity is essential to write correct, portable, and efficient programs. Improper use may lead to undefined behaviour, making careful usage critical in C programming.
📘 Reference
Prepared strictly from
“Computer Fundamentals and Programming in C”
Pradip Dey & Manas Ghosh
Oxford University Press, Second Edition
Aivette-COI
Learn smart. Write clean. Score high.
With love 🤍
— Aivette
Comments
Post a Comment