Apply for Zend Framework Certification Training

c

< Purpose of int main(),char main(),void main() with examples C Operator Precedence Quiz >




Order of Precedence in C
Operator precedence determines which operator is evaluated first in an expression when multiple operators are used.
For example:
int result = 10 + 5 * 2;
Here, multiplication (*) has higher precedence than addition (+).
Evaluation:
10 + (5 * 2)
10 + 10
20
Therefore, the result is 20, not 30.
1. C Operator Precedence Table
The following table is arranged from highest precedence to lowest precedence.
Priority    Operators          Description                                                                 Associativity
1            () [] -> .                    Function call, array access, structure member access   Left to Right
2             ++ -- (postfix)          Post-increment, post-decrement                           Left to Right
3            ++ -- (prefix), + -, !, ~, (type), *, &, sizeof      Unary operators             Right to Left
4             * / %                       Multiplication, division, modulus                           Left to Right
5            + -                            Addition, subtraction                                           Left to Right
6            << >>                      Bitwise left and right shift                                    Left to Right
7            < <= > >=               Relational operators                                            Left to Right
8             == !=                      Equality operators                                               Left to Right
9             &                             Bitwise AND                                                       Left to Right
10           ^                             Bitwise XOR                                                       Left to Right
11            `                              `                                                                     Bitwise OR
12           &&                           Logical AND                                                        Left to Right
13            `                                                                                                     `
14           ?:                            Conditional (ternary) operator                              Right to Left
15           =, +=, -=, *=, /=, %= Assignment operators                                      Right to Left
16                                          Comma operator                                                 Left to Right
 
Note: Associativity determines the evaluation direction when operators have the same precedence. Parentheses can be used to explicitly control the order.
2. Examples of Operator Precedence
Example 1: Multiplication Before Addition
#include<stdio.h>
int main(){
    int result = 10 + 5 * 2;
    printf("Result = %d", result);
    return 0;
}
Output:
Result = 20
Explanation:
10 + (5 * 2)
10 + 10 = 20
Example 2: Division and Multiplication
#include<stdio.h>
int main(){
    int result = 20 / 5 * 2;
    printf("Result = %d", result);
    return 0;
}
Output:
Result = 8
Explanation:
/ and * have the same precedence, so associativity is left to right.
(20 / 5) * 2
4 * 2 = 8
Example 3: Parentheses Change Precedence
#include<stdio.h>
int main(){
    int a = 10 + 5 * 2;
    int b = (10 + 5) * 2;
    printf("a = %d\n", a);
    printf("b = %d", b);
    return 0;
}
Output:
a = 20
b = 30
Explanation:
a = 10 + (5 * 2) = 20
b = (10 + 5) * 2 = 30
Example 4: Relational Before Logical AND
#include<stdio.h>
int main(){
    int result = 5 > 3 && 10 < 20;
    printf("Result = %d", result);
    return 0;
}
Output:
Result = 1
Explanation:
Relational operators have higher precedence than logical AND.
(5 > 3) && (10 < 20)
1 && 1
1
Example 5: Logical AND Before Logical OR
#include<stdio.h>
int main(){
    int result = 0 || 1 && 0;
    printf("Result = %d", result);
    return 0;
}
Output:
Result = 0
Explanation:
&& has higher precedence than ||.
0 || (1 && 0)
0 || 0
0
Example 6: Assignment Associativity
Assignment operators have right-to-left associativity.
#include<stdio.h>
int main(){
    int a, b, c;
    a = b = c = 10;
    printf("%d %d %d", a, b, c);
    return 0;
}
Output:
10 10 10
Explanation:
a = (b = (c = 10))
First, c receives 10, then b, and finally a.
Example 7: Increment Operator
#include<stdio.h>
int main(){
    int a = 5;
    int result = ++a * 2;
    printf("a = %d\n", a);
    printf("Result = %d", result);
    return 0;
}
Output:
a = 6
Result = 12
Explanation:
Prefix increment has higher precedence than multiplication.
++a → 6
6 * 2 = 12
Example 8: Ternary Operator
#include<stdio.h>
int main(){
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    printf("Maximum = %d", max);
    return 0;
}
Output:
Maximum = 20
Explanation:
(a > b) ? a : b
10 > 20 → false
Therefore, b is selected.
3. Important Rules to Remember
Parentheses () can be used to change the evaluation order.
Multiplication, division, and modulus have higher precedence than addition and subtraction.
Operators with the same precedence follow associativity.
Most binary arithmetic operators follow left-to-right associativity.
Assignment operators follow right-to-left associativity.
Precedence does not always define the order in which operands are evaluated. For example, function argument evaluation order is generally unspecified in C.
Quick Example
int result = 2 + 3 * 4 - 6 / 2;
Step-by-step:
2 + (3 * 4) - (6 / 2)
2 + 12 - 3
14 - 3
11
Final Result: 11

< Purpose of int main(),char main(),void main() with examples C Operator Precedence Quiz >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top