precedence of operators in C programming

Operator Precedence in C

Operator Precedence in C

In C programming, operator precedence determines the order in which operators are evaluated in expressions. Understanding precedence is crucial for writing correct and efficient code. Operators with higher precedence are evaluated before those with lower precedence. Here’s a summary of operator precedence in C:

Operator Precedence Table

Precedence Operators Description
1 (), [], ., -> Function call, array subscript, member access
2 ++, --, +, -, ~, ! Postfix increment/decrement, prefix increment/decrement, unary plus/minus, bitwise NOT, logical NOT
3 *, /, % Multiplication, division, modulus
4 +, - Addition, subtraction
5 <, >, <=, >= Relational operators
6 ==, != Equality operators
7 && Logical AND
8 || Logical OR
9 ? : Conditional (ternary) operator
10 =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>= Assignment operators
11 , Comma operator

Examples

Consider the following expression:

int a = 5 + 3 * 2;

In this expression, the multiplication operator * has higher precedence than the addition operator +, so 3 * 2 is evaluated first, and then 5 + 6 is evaluated, resulting in a = 11.

For complex expressions, understanding operator precedence helps avoid logical errors and ensures that operations are performed in the intended order.

Note: Operator precedence is crucial for writing correct expressions. Always use parentheses to make the order of operations explicit and improve code readability.

Comments

Popular Posts