Operator Precedence In C

  • Postfix increment/decrement (++/--): These operators increase or decrease the value of a variable by 1 after the expression is evaluated.
  • Prefix increment/decrement (++/--): These operators increase or decrease the value of a variable by 1 before the expression is evaluated.
  • Unary plus/minus (+/-): These operators are used to specify the sign of a value.
  • Logical negation (!): The logical negation (!) operator is used to make a boolean expression's logical meaning the opposite.
  • Bitwise complement (~): This operator is used to flip the bits of a value.
  • Casts: These operators are employed to change an expression's type into another type.
  • Multiplication/division/modulo (* / %): These operators perform multiplication, division, and modulo operations.
  • Addition/subtraction (+ -): These operators perform addition and subtraction operations.
  • Bitwise shift (<< >>): These operators are employed to left- or right-shift the bits of a number.
  • Relational operators (< <= > >=): These operators perform a comparison between two numbers and provide a boolean result as a result.
  • The equality operators (==!=): These operators compare two values to see if they are equal and then deliver a boolean result based on the comparison
  • Bitwise AND (&): This operator combines two numbers in a bitwise manner.
  • Bitwise XOR : Using this operator, two numbers are bitwise XORed.
  • Bitwise OR (|): This operator combines two numbers in a bitwise manner.
  • Logical AND (&&): The logical AND operator (&&) applies the logical AND function to two boolean values.
  • Logical OR (||): Using this operator, two boolean numbers are logically combined. Ternary conditional operator (?:): This operator allows for a conditional statement that is used to evaluate one of two expressions based on the result of a boolean expression.
  • Assignment (= += -= *= /= %= <<= >>= &= ^= |=): These operators are used to assign a value to a variable, or to modify the value of a variable.

It is crucial to understand that the sequence of evaluation may not always be obvious or direct, requiring the use of parentheses to specify the desired sequence of actions. To illustrate, take the expression "2 + 3 4". In the absence of parentheses, it is ambiguous whether the multiplication or addition takes precedence. Yet, by writing "(2 + 3) 4", the order of operations becomes evident: first, addition is executed, followed by multiplication.

Besides operator precedence, C programming language also defines rules for operator associativity. This concept specifies the order in which operators of equal precedence are processed. For instance, the plus and minus operators share the same precedence level, but they follow a left-associative behavior. This implies that they are computed from left to right. Thus, the expression "2 + 3 - 4" is computed as "(2 + 3) - 4", resulting in a value of 1.

Understanding operator precedence and associativity in the C programming language is crucial because they have a significant impact on the evaluation of expressions. Proper utilization of these rules is essential to avoid unexpected outcomes and ensure the accuracy and efficiency of your code.

In C programming, operators possess associativity, determining the sequence in which operators of equal precedence are computed. The two types of associativity are left-to-right and right-to-left.

Here is a table of operator associativity in C:

Operators with a left-to-right associativity are assessed in a sequence from left to right, whereas operators with a right-to-left associativity are assessed from right to left. This distinction can impact the sequence in which operations are executed when an expression includes multiple operators with equal precedence.

To understand operator precedence in C language, let's examine the following example:

C Program:

Example

#include <stdio.h>

int main() {
    int x = 2, y = 3, z = 4;
    int result = x + y * z; // result = 14
    printf("Result: %d\n", result);
    
    int a = 5, b = 6, c = 7;
    int result2 = (a + b) * c; // result2 = 77
    printf("Result2: %d\n", result2);
    
    int d = 8, e = 9, f = 10;
    int result3 = d + e % f; // result3 = 17
    printf("Result3: %d\n", result3);
    
    int g = 11, h = 12, i = 13;
    int result4 = g * h / i; // result4 = 10
    printf("Result4: %d\n", result4);
    
    int j = 14, k = 15, l = 16;
    int result5 = j == k || l > k; // result5 = 1 (true)
    printf("Result5: %d\n", result5);
    
    return 0;
}

Output

Output

Result: 14
Result2: 77
Result3: 17
Result4: 10
Result5: 1
  • In the first example, the expression x + y z is evaluated as x + (y z), due to the higher precedence of * over +. This results in result being assigned the value of 14.
  • In the second example, the expression (a + b) c is evaluated as (a + b) c, due to the parentheses overriding the precedence of + and *. This results in result2 being assigned the value of 77.
  • In the third example, the expression e % f is evaluated first, due to the higher precedence of % over +, and then the result is added to d. This results in result3 being assigned the value of 17.
  • In the fourth example, the expression g h is evaluated first, due to the same precedence of and / and left-to-right associativity. Then the result is divided by i. This results in result4 being assigned the value of 10.
  • In the fifth example, the expression j == k is evaluated first, due to the same precedence of == and || and left-to-right associativity. Then the result is OR-ed with the result of l > k. This results in result5 being assigned the value of 1 (true).
  • Conclusion

In summary, having a grasp of operator precedence and associativity in C is essential for developing accurate and optimized code. This knowledge determines the order in which operators are processed and can impact the result of an expression. It is important to remember that parentheses have the ability to alter the default precedence and associativity of operators. By leveraging the operator precedence and associativity tables alongside parentheses, you can create intricate expressions that yield the intended result.

Input Required

This code uses input(). Please provide values below: