C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Precedence And Associativity Of C Programming Language Operators

In the C programming language, operators have different levels of precedence, which determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence. In addition, operators with the same precedence may have left-to-right or right-to-left associativity, which determines the order in which they are evaluated when they appear in a sequence.

The following is a list of the precedence and associativity of the most common operators in the C programming language, from highest to lowest precedence:

OperatorDescriptionAssociativity
()Function callLeft-to-right
[]Array subscriptingLeft-to-right
.Structure and union member accessLeft-to-right
->Structure and union member access through pointerLeft-to-right
++ --Postfix increment and decrementLeft-to-right
+ - ! ~ ++ --Unary plus, minus, logical not, bitwise not, prefix increment and decrementRight-to-left
* / %Multiplication, division, modulusLeft-to-right
+ -Addition, subtractionLeft-to-right
<< >>Bitwise left shift, right shiftLeft-to-right
< <= > >=Relational operatorsLeft-to-right
== !=Equality operatorsLeft-to-right
&Bitwise ANDLeft-to-right
^Bitwise exclusive ORLeft-to-right
``Bitwise OR
&&Logical ANDLeft-to-right
``
?:Ternary conditional operatorRight-to-left
= `+= -= *= /= %= &= ^== <<= >>=`Assignment operators
,Comma operatorLeft-to-right

When evaluating an expression, it is important to keep the precedence and associativity of the operators in mind, as it can affect the order in which the expressions are evaluated and the final result of the expression. It is also possible to use parentheses to override the default precedence and explicitly specify the order of evaluation.

  1. Operator Precedence Tables in C Language:

    Operator precedence determines the order in which operators are evaluated in an expression. Here's a basic overview:

    • Highest Precedence: () (parentheses)
    • ...
    • Lowest Precedence: = (assignment)
    #include <stdio.h>
    
    int main() {
        int result = 5 + 3 * 2; // Multiplication has higher precedence than addition
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: 11
    
  2. Changing Precedence with Parentheses in C Programming:

    #include <stdio.h>
    
    int main() {
        int result = (5 + 3) * 2; // Parentheses change precedence
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: 16
    

    Parentheses can be used to override the default precedence.

  3. C Code Examples Demonstrating Operator Precedence:

    #include <stdio.h>
    
    int main() {
        int result = 10 / 2 + 3 * 2; // Division and multiplication have higher precedence
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: 11
    

    This example showcases the precedence of division, multiplication, and addition.

  4. Associativity of Binary Operators in C:

    #include <stdio.h>
    
    int main() {
        int result = 5 - 3 - 1; // Left-to-right associativity
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: 1
    

    Binary operators with the same precedence are evaluated from left to right.

  5. Unary Operators and Their Precedence in C:

    #include <stdio.h>
    
    int main() {
        int num = 5;
        int result = -num; // Unary minus has higher precedence than binary operators
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: -5
    

    Unary operators like - have higher precedence.

  6. Comparison of Logical and Relational Operator Precedence in C:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10, c = 15;
        int result = (a < b) && (b < c); // Logical AND has lower precedence than relational operators
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: 1
    

    Relational operators (<, >, etc.) have higher precedence than logical operators (&&, ||, etc.).

  7. Bitwise Operators and Their Precedence in C Programming:

    #include <stdio.h>
    
    int main() {
        int a = 5, b = 10;
        int result = (a & b) ^ (a | b); // Bitwise AND, OR, and XOR have different precedence
        printf("Result: %d\n", result);
        return 0;
    }
    

    Output:

    Result: 15
    

    Bitwise operators have distinct precedence levels.