C ProgrammingUnit 44 min read
Operators and Expressions: Types, Precedence, and Associativity
Unit 4 of C Programming: This unit provides a comprehensive overview of C operators, including arithmetic, relational, logical, bitwise, and assignment operators, while detailing the critical rules of operator precedence and associativity that govern expression evaluation.
Key points
- C operators are symbols that perform specific mathematical or logical manipulations on operands.
- Expressions are combinations of constants, variables, and operators that evaluate to a single value.
- Operator precedence determines the order of evaluation, while associativity defines the direction (left-to-right or right-to-left).
- Increment and decrement operators exist in prefix and postfix forms, which differ in their timing of value modification.
- Bitwise operators allow for low-level manipulation of data at the binary bit level.
Introduction to Operators and Expressions
An operator is a symbol that tells the compiler to perform specific mathematical, relational, or logical manipulations. The data items upon which operators act are called operands. An expression is a sequence of operators and operands that specifies a computation.
Classification of Operators
C supports a rich set of operators, which can be categorized as follows:
1. Arithmetic Operators
These are used for numerical calculations.
+(Addition),-(Subtraction),*(Multiplication),/(Division),%(Modulus).- Note: The
%operator can only be used with integers.
2. Relational Operators
Used to compare two values. They return 1 (true) or 0 (false).
==(Equal to),!=(Not equal to),>(Greater than),<(Less than),>=(Greater than or equal to),<=(Less than or equal to).
3. Logical Operators
Used to combine multiple conditions.
&&(Logical AND),||(Logical OR),!(Logical NOT).
4. Assignment Operators
Used to assign values to variables.
- Simple:
= - Compound:
+=,-=,*=,/=,%=. (e.g.,a += 5is equivalent toa = a + 5).
5. Increment and Decrement Operators
These are unary operators used to increase or decrease a value by 1.
- Prefix (
++a,--a): The value is changed first, then used in the expression. - Postfix (
a++,a--): The current value is used in the expression first, then changed.
Example:
int a = 5, b;
b = ++a; // a becomes 6, b becomes 6
b = a++; // b becomes 6, a becomes 7
6. Bitwise Operators
These perform operations at the bit level.
&(AND),|(OR),^(XOR),~(NOT),<<(Left shift),>>(Right shift).
7. Conditional (Ternary) Operator
The ?: operator is a shorthand for if-else statements.
- Syntax:
condition ? expression1 : expression2; - If the condition is true,
expression1is executed; otherwise,expression2is executed.
Operator Precedence and Associativity
When an expression contains multiple operators, the compiler follows a specific hierarchy:
| Category | Operators | Associativity |
|---|---|---|
| Postfix | () [] -> . ++ -- |
Left to Right |
| Unary | + - ! ~ ++ -- * & (type) sizeof |
Right to Left |
| Multiplicative | * / % |
Left to Right |
| Additive | + - |
Left to Right |
| Relational | < <= > >= |
Left to Right |
| Equality | == != |
Left to Right |
| Logical AND | && |
Left to Right |
| Logical OR | ` | |
| Conditional | ?: |
Right to Left |
| Assignment | = += -= *= /= %= |
Right to Left |
Precedence: Decides which operator is evaluated first. Associativity: Decides the direction of evaluation when operators have the same precedence.
Worked Example: Expression Evaluation
Evaluate: x = 5 + 3 * 2 / 4 % 2;
- Multiplication (
*) and Division (/) have higher precedence than+. 3 * 2 = 66 / 4 = 1(Integer division)1 % 2 = 15 + 1 = 6x = 6
Comparison Table: Operators
| Feature | Logical AND (&&) |
Bitwise AND (&) |
|---|---|---|
| Purpose | Logical comparison | Bit-by-bit manipulation |
| Operand Type | Boolean/Integer | Integer types only |
| Result | 0 or 1 | Result of bitwise operation |
Exam Tip
- Always show the difference between Prefix and Postfix: Examiners frequently ask for a code snippet demonstrating the difference.
- Precedence Tables: You do not need to memorize the entire table, but you must know the order: Unary > Arithmetic > Relational > Logical > Assignment.
- Bitwise Questions: If asked about bitwise operators, always include a small binary conversion example (e.g.,
5 & 3where0101 & 0011 = 0001). - Conditional Operator: When asked about the conditional operator, provide a clear syntax and a simple example like finding the maximum of two numbers.
Based on the TU BSc CSIT syllabus for C Programming (CSC115), unit 4.
Discussion
Loading…