C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
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:
Operator | Description | Associativity |
---|---|---|
() | Function call | Left-to-right |
[] | Array subscripting | Left-to-right |
. | Structure and union member access | Left-to-right |
-> | Structure and union member access through pointer | Left-to-right |
++ -- | Postfix increment and decrement | Left-to-right |
+ - ! ~ ++ -- | Unary plus, minus, logical not, bitwise not, prefix increment and decrement | Right-to-left |
* / % | Multiplication, division, modulus | Left-to-right |
+ - | Addition, subtraction | Left-to-right |
<< >> | Bitwise left shift, right shift | Left-to-right |
< <= > >= | Relational operators | Left-to-right |
== != | Equality operators | Left-to-right |
& | Bitwise AND | Left-to-right |
^ | Bitwise exclusive OR | Left-to-right |
` | ` | Bitwise OR |
&& | Logical AND | Left-to-right |
` | ` | |
?: | Ternary conditional operator | Right-to-left |
= `+= -= *= /= %= &= ^= | = <<= >>=` | Assignment operators |
, | Comma operator | Left-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.
Operator Precedence Tables in C Language:
Operator precedence determines the order in which operators are evaluated in an expression. Here's a basic overview:
()
(parentheses)=
(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
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.
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.
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.
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.
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.).
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.