C Operator Precedence - cppreference.com (2024)

C

Language
Headers
Type support
Program utilities
Variadic function support
Error handling
Dynamic memory management
Strings library
Algorithms
Numerics
Date and time utilities
Input/output support
Localization support
Concurrency support (C11)
Technical Specifications
Symbol index

[edit]

C language

Basic concepts
Keywords
Preprocessor
Statements
Expressions
Initialization
Declarations
Functions
Miscellaneous
History of C
Technical Specifications

[edit]

Expressions

General
value category
evaluation order and sequence points
constant expressions
implicit conversions
generic selection (C11)
constants and literals
integer constant
floating constant
character constant

true/false

(C23)

nullptr

(C23)

string literal
compound literal (C99)
operators
operator precedence
member access and indirection
logical operators
comparison operators
arithmetic operators
assignment operators
increment and decrement
function call, comma, conditional operator

sizeof

_Alignof

(C11)

cast operators

[edit]

The following table lists the precedence and associativity of C operators. Operators are listed top to bottom, in descending precedence.

Precedence Operator Description Associativity
1 ++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal(C99)
2 ++ -- Prefix increment and decrement[note 1] Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Cast
* Indirection (dereference)
& Address-of
sizeof Size-of[note 2]
_Alignof Alignment requirement(C11)
3 * / % Multiplication, division, and remainder Left-to-right
4 + - Addition and subtraction
5 << >> Bitwise left shift and right shift
6 < <= For relational operators < and ≤ respectively
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional[note 3] Right-to-left
14[note 4] = Simple assignment
+= -= Assignment by sum and difference
*= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right
  1. The operand of prefix ++ and -- can't be a type cast. This rule grammatically forbids some expressions that would be semantically invalid anyway. Some compilers ignore this rule and detect the invalidity semantically.
  2. The operand of sizeof can't be a type cast: the expression sizeof (int) * p is unambiguously interpreted as (sizeof(int)) * p, but not sizeof((int)*p).
  3. The expression in the middle of the conditional operator (between ? and :) is parsed as if parenthesized: its precedence relative to ?: is ignored.
  4. Assignment operators' left operands must be unary (level-2 non-cast) expressions. This rule grammatically forbids some expressions that would be semantically invalid anyway. Many compilers ignore this rule and detect the invalidity semantically. For example, e = a < d ? a++ : a = d is an expression that cannot be parsed because of this rule. However, many compilers ignore this rule and parse it as e = ( ((a < d) ? (a++) : a) = d ), and then give an error because it is semantically invalid.

When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expression *p++ is parsed as *(p++), and not as (*p)++.

Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a=b=c is parsed as a=(b=c), and not as (a=b)=c because of right-to-left associativity.

[edit] Notes

Precedence and associativity are independent from order of evaluation.

The standard itself doesn't specify precedence levels. They are derived from the grammar.

In C++, the conditional operator has the same precedence as assignment operators, and prefix ++ and -- and assignment operators don't have the restrictions about their operands.

Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always associate right-to-left (sizeof ++*p is sizeof(++(*p))) and unary postfix operators always associate left-to-right (a[1][2]++ is ((a[1])[2])++). Note that the associativity is meaningful for member access operators, even though they are grouped with unary postfix operators: a.b++ is parsed (a.b)++ and not a.(b++).

[edit] References

  • C17 standard (ISO/IEC 9899:2018):
  • A.2.1 Expressions
  • C11 standard (ISO/IEC 9899:2011):
  • A.2.1 Expressions
  • C99 standard (ISO/IEC 9899:1999):
  • A.2.1 Expressions
  • C89/C90 standard (ISO/IEC 9899:1990):
  • A.1.2.1 Expressions

[edit] See also

Order of evaluation of operator arguments at run time.

Common operators
assignment incrementdecrement arithmetic logical comparison memberaccess other

a = b
a += b
a -= b
a *= b
a /= b
a %= b
a &= b
a |= b
a ^= b
a <<= b
a >>= b

++a
--a
a++
a--

+a
-a
a + b
a - b
a * b
a / b
a % b
~a
a & b
a | b
a ^ b
a << b
a >> b

!a
a && b
a || b

a == b
a != b
a < b
a > b
a <= b
a >= b

a[b]
*a
&a
a->b
a.b

a(...)
a, b
(type) a
a ? b : c
sizeof

_Alignof
(since C11)

C++ documentation for C++ operator precedence

C Operator Precedence - cppreference.com (2024)

FAQs

What is the precedence of dereference operator in C? ›

In C/C++, precedence of Prefix ++ (or Prefix –) has same priority than dereference (*) operator, and precedence of Postfix ++ (or Postfix –) is higher than both Prefix ++ and *. If p is a pointer then *p++ is equivalent to *(p++) and ++*p is equivalent to ++(*p) (both Prefix ++ and * are right associative).

What is the priority among (* %) (+ -) and (=) C operators? ›

operator precedence
PrecedenceOperatorDescription
3* / %Multiplication, division, and remainder
4+ -Addition and subtraction
5<< >>Bitwise left shift and right shift
6< <=For relational operators < and ≤ respectively
28 more rows
Jul 31, 2023

What is the operator precedence rule in C++? ›

Operator precedence specifies the order of operations in expressions that contain more than one operator. Operator associativity specifies whether, in an expression that contains multiple operators with the same precedence, an operand is grouped with the one on its left or the one on its right.

What is correct order of precedence in C? ›

Precedence and associativity of C operators
Symbol 1Type of operationAssociativity
typecastsUnaryRight to left
* / %MultiplicativeLeft to right
+ -AdditiveLeft to right
<< >>Bitwise shiftLeft to right
12 more rows
Aug 2, 2021

What is the precedence of dereference and increment operator? ›

++ is increment operator used in prefix and postfix expressions. * is dereference operator. Precedence of prefix ++ and * is same and both are right to left associative. Precedence of postfix ++ is higher than both prefix ++ and * and is left to right associative.

Which operator has the highest precedence in C? ›

++ and -- (increment and decrement) operators hold the highest precedence. Then comes * , / and % holding equal precedence. And at last, we have the + and - operators used for addition and subtraction, with the lowest precedence.

How to remember operator precedence in C? ›

Here, we have four operators, in which the / and * operators have the same precedence but have higher precedence than the + and – operators. So, according to the Left-to-Right associativity of / and *, / will be evaluated first. Now, between + and –, + will be evaluated due to Left-to-Right associativity.

Which operator has the lowest precedence in C? ›

The primary operators have the highest precedence. The comma operator has the lowest precedence.

Which ______ operator has the highest precedence? ›

Operator precedence specifies the manner in which operands are grouped with operators. For example, 1 + 2 * 3 is treated as 1 + (2 * 3) , whereas 1 * 2 + 3 is treated as (1 * 2) + 3 because the multiplication operator * has a higher precedence than the addition operator + .

What is the correct operator precedence? ›

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3 , the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary.

What are the 5 precedence of logical operators? ›

Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to not , followed by and , and finally by or .

Which operator cannot be overloaded? ›

The class member selector operator or Dot(.) operator cannot be overloaded and it will throw an error as seen in the output. Overloading the Dot(.) operator will arise a conflict in the meaning as the operator may be used for object overloading or to refer to an object of the class.

What is the hierarchy of operators in C? ›

The precedence rules for C arithmetic operators are as follows: Operators within parentheses are evaluated first. Unary operators (++ and --) have higher precedence than other arithmetic operators. Multiplication, division, and modulus operators have higher precedence than addition and subtraction operators.

What is the precedence rule? ›

Precedence rules are defined in terms of a trigger attribute and a target attribute, where a user's selection of the trigger reveals the previously unavailable target attribute to the user.

Which logical operator has the highest precedence? ›

So, a reasonable question is “which logical operation is performed first?” The order of precedence is: logical complements ( Not ) are performed first, logical conjunctions ( And ) are performed next, and logical disjunctions ( Or ) are performed at the end.

What is the order of execution in C? ›

The steps of execution of C program are C code then Preprocessing then Compiler then Assembler then Linker then Loader. In Preprocessing source code is attached to the preprocessor file.

What is the precedence of increment and decrement operators in C? ›

For increment and decrement operators in C, the associativity is left to right. That is, when an expression has pre-increment and pre-decrement operators, then the leftmost operator will be evaluated first, and so on, depending upon the placement in code.

What is the priority of C logical operators? ›

Precedence of Logical Operators in C

The order of precedence for logical operators in C is as follows: NOT operator (!) AND operator (&&) OR operator (||)

Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5357

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.