C Operator Precedence 🎯

beginner
12 min

C Operator Precedence 🎯

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! 🎉

What are Operators in C? 📝

Operators in C are symbols that tell the compiler to perform specific operations on variables and values. They can be categorized into several types:

  • Arithmetic Operators: +, -, *, /, %
  • Assignment Operators: =, +=, -=, *=, /=, %=
  • Comparison Operators: <, <=, >, >=, ==, !=
  • Logical Operators: &&, ||, !
  • Bitwise Operators: &, |, ^, ~, <<, >>
  • Miscellaneous Operators: ,, ., ->, sizeof, &, *, (), []

Operator Precedence in C 💡

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:

c
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 💡

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:

  • Left-to-right associativity: Operators associate from left to right, i.e., they group from left to right. For example, a + b + c is evaluated as (a + b) + c.
  • Right-to-left associativity: Operators associate from right to left, i.e., they group from right to left. However, this type of associativity is rare in C.

C Operator Precedence Table 💡

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 (,)

Example 1: Understanding Operator Precedence 💡

Let's consider the following example to understand operator precedence:

c
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.

Example 2: Understanding Operator Associativity 💡

Here's an example to illustrate operator associativity:

c
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.

Quick Quiz
Question 1 of 1

Which operator has the highest precedence among `+`, `*`, and `/` in C?

Quick Quiz
Question 1 of 1

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! 🤓🚀