Welcome to our comprehensive guide on C Operator Precedence! In this lesson, we'll delve into the world of operators in C programming, focusing on their precedence and associativity. Let's get started! 🎉
Operators in C are symbols that tell the compiler to perform specific operations on variables and values. They can be categorized into several types:
+, -, *, /, %=, +=, -=, *=, /=, %=<, <=, >, >=, ==, !=&&, ||, !&, |, ^, ~, <<, >>,, ., ->, sizeof, &, *, (), []Operator precedence in C determines the order in which operators are evaluated in an expression. In other words, it tells us which operation should be performed first when multiple operators are involved.
Here's an example expression to illustrate operator precedence:
int a = 10;
int b = 20;
int c = a * b + 5;In this example, the multiplication operation (*) is performed first because it has higher precedence than the addition operation (+).
Operator associativity in C determines the grouping of terms when multiple operators of the same precedence occur in an expression. There are two types of associativity:
a + b + c is evaluated as (a + b) + c.To understand the precedence of operators in C, refer to the following table:
1. Primary Expressions
- Parentheses ()
- Function Calls
- Pointers and Indirection Operator (->, .)
- Sizeof Operator
2. Unary Operators
- Postfix Increment and Decrement (++, --)
- Prefix Increment and Decrement (++, --)
- Logical Not (!)
- Bitwise NOT (~)
- Address Operator (&)
- Indirection Operator (*)
- Cast Operator ((type))
- Sizeof Operator (sizeof)
- Pointer Arithmetic
3. Multiplicative Operators
- Multiplication (*)
- Division (%, /)
- Modulus (%)
4. Additive Operators
- Addition (+)
- Subtraction (-)
5. Shift Operators
- Left Shift (<<)
- Right Shift (>>)
6. Relational Operators
- Equal (==)
- Not Equal (!=)
- Less Than (<)
- Less Than or Equal (<=)
- Greater Than (>)
- Greater Than or Equal (>=)
7. Equality Operators
- Identical (==, ===)
- Not Identical (!=, !==)
8. Bitwise AND (&)
9. Bitwise XOR (^)
10. Bitwise OR (|)
11. Logical AND (&&)
12. Logical OR (||)
13. Conditional Operator (?:)
14. Assignment Operators
- Simple Assignment (=)
- Compound Assignment (+=, -=, *=, /=, %=, <<=, >>=)
15. Comma Operator (,)
Let's consider the following example to understand operator precedence:
int a = 10;
int b = 20;
int c = a * b + 5 * 2;In this example, the multiplication operation (*) has higher precedence than the addition operation (+). So, the expression a * b is evaluated first, and then the result is added to 5 * 2.
Here's an example to illustrate operator associativity:
int a = 10;
int b = 20;
int c = 30;
int sum = a + b * c;In this example, the multiplication operation (*) has higher precedence than the addition operation (+), but they have the same associativity (left-to-right). So, the expression b * c is evaluated first, and then the result is added to a.
Which operator has the highest precedence among `+`, `*`, and `/` in C?
Which of the following operators associates from right to left in C?
By now, you should have a good understanding of operator precedence and associativity in C. Practice writing expressions and identifying the order of operations to reinforce your understanding. Happy coding! 🤓🚀