🌐
TutorialsPoint
tutorialspoint.com › home › cprogramming › c operators precedence
Operator Precedence in C
June 10, 2012 - Both the "/" (division) and "*" (multiplication) operators have the same precedence, so the order of evaluation will be decided by associativity. As per the above table, the associativity of the multiplicative operators is from Left to Right.
🌐
Programiz
programiz.com › c-programming › precedence-associativity-operators
C Precedence And Associativity Of Operators: Definition and Examples
Become a certified C programmer. Try Programiz PRO! ... The precedence of operators determines which operator is executed first if there is more than one operator in an expression.
Discussions

Operator precedence table for the C programming language - Stack Overflow
What would a correct operator precedence table that lists all operators in the C language look like? I have made extensive searches on the web, and found many such precedence tables. Alas, I haven't More on stackoverflow.com
🌐 stackoverflow.com
syntax - What should be the precedence of the bitwise operators relative to each others? - Programming Language Design and Implementation Stack Exchange
In C, the relative precedence of bitwise operators is as follows, from high to low precedence: ~ bitwise NOT bitwise left shift and right shift & bitwise AND ^ ... More on langdev.stackexchange.com
🌐 langdev.stackexchange.com
Why are the usual operator precedence rules are the way they are?
The (not, and, or) triple is kinda like (unary -, *, +) triple except for booleans. This is how the logic expressions are usually written in math (DNF) and programming languages just copy that. As to why it is that way in math, I think that in most cases, it reduces the amount of parentheses needed. More on reddit.com
🌐 r/ProgrammingLanguages
39
14
October 19, 2022
Precedence of logical operators and parantheses
C has short-circuiting logical operators, so the left hand of the && will be fully evaluated before the right. If the left part results in a false, the right hand won't be evaluated at all. More on reddit.com
🌐 r/C_Programming
9
9
March 6, 2023
🌐
PVS-Studio
pvs-studio.com › en › blog › terms › 0064
Operator precedence in C and C++
July 19, 2021 - Operations are executed in a strict order. The value that determines a privilege to execute a certain operation is called precedence. The operation execution order can be regulated by use of parentheses.
built-in and (in C++) overloadable functions
This is a list of operators in the C and C++ programming languages. All listed operators are in C++ and lacking indication otherwise, in C as well. Some tables include a "In … Wikipedia
🌐
Wikipedia
en.wikipedia.org › wiki › Operators_in_C_and_C++
Operators in C and C++ - Wikipedia
3 weeks ago - The order of precedence table resolves the final sub-expression they each act upon: ( . )[ i ] acts only on y, ( . )++ acts only on y[i], 2*( . ) acts only on y[i]++ and 3+( . ) acts 'only' on 2*((y[i])++). It is important to note that WHAT sub-expression gets acted on by each operator is clear ...
🌐
IBM
ibm.com › docs › en › wdfrhcw › 1.4.0
C operators and operands
We cannot provide a description for this page right now
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › precedence-and-order-of-evaluation
Precedence and order of evaluation | Microsoft Learn
August 3, 2021 - An expression can contain several operators with equal precedence. When several such operators appear at the same level in an expression, evaluation proceeds according to the associativity of the operator, either from right to left or from left ...
🌐
Medium
medium.com › @mahdinacademy › operators-and-operator-precedence-in-c-programming-c47b078c2dca
Operators and Operator Precedence in C Programming | by Mahdin Academy | Medium
July 13, 2023 - If true, it assigns the string "Adult" to the result variable; otherwise, it assigns "Minor". Operator precedence determines the order in which operators are evaluated when an expression contains multiple operators.
Find elsewhere
🌐
Northwestern
users.eecs.northwestern.edu › ~wkliao › op-prec.htm
C Operator Precedence Table
Dynamically Adapting File Domain Partitioning Methods for Collective I/O Based on Underlying Parallel File System Locking Protocols. In the Proceedings of International Conference for High Performance Computing, Networking, Storage and Analysis, Austin, Texas, November 2008.
🌐
Cppreference
en.cppreference.com › w › c › language › operator_precedence.html
C Operator Precedence - cppreference.com
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.
Top answer
1 of 3
8


Explanation

Prec. denotes operator precedence, where group 1 has the highest precedence and group 17 the lowest.

Assoc. denotes operator associativity, where such is applicable. Associativity can be either left-to-right or right-to-left.

Sources

My ambition with this post is to provide a operator precedence table on-site at Stack Overflow, which is correct and canonical. This operator precedence table corresponds directly to chapter 6.5 of ISO 9899:2011, where we can read (6.5/3):

The grouping of operators and operands is indicated by the syntax. 85)

And then as a comment, in the informative (not normative) foot note:

85) The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of the major subclauses of this subclause, highest precedence first. /--/

Within each major subclause, the operators have the same precedence. Left- or right-associativity is indicated in each subclause by the syntax for the expressions discussed therein.

All formal operator names from the table are taken from chapter 6.5, where such a name could be found in normative text. Informal names were included in the cases where the programmer community might be more familiar with another name than the one given in the standard.

2 of 3
1

Here:

http://basen.oru.se/c/operators.html

(I added _Alignof, which I think is the only new operator in C11, to my own table, and published it there. Maybe that's cheating? Comments and suggestions on how to improve the table are welcome.)

🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › cpp › cpp-built-in-operators-precedence-and-associativity
C++ built-in operators, precedence, and associativity | Microsoft Learn
August 3, 2021 - The C++ language includes all C ... on one or more operands. Operator precedence specifies the order of operations in expressions that contain more than one operator....
🌐
Swansontec
swansontec.com › sopc.html
C Language Operator Precedence Chart
Operator precedence describes the order in which C reads expressions. For example, the expression a=4+b*2 contains two operations, an addition and a multiplication. Does the C compiler evaluate 4+b first, then multiply the result by 2, or does it evaluate b*2 first, then add 4 to the result?
🌐
GeeksforGeeks
geeksforgeeks.org › c language › operator-precedence-and-associativity-in-c
Operator Precedence and Associativity in C - GeeksforGeeks
In C, expressions often contain ... precedence and associativity are rules that decide which operator is applied first and in which direction operators of the same precedence are evaluated....
Published   November 1, 2025
🌐
W3Schools
w3schools.com › c › c_operators_precedence.php
C Operator Precedence
C Examples C Real-Life Examples ... Q&A C Certificate ... When a calculation contains more than one operator, C follows order of operations rules to decide which part to calculate first....
🌐
Milwaukee School of Engineering
faculty-web.msoe.edu › johnsontimoj › Common › FILES › precedence.pdf pdf
C – Operator Precedence Precedence Operator Description Associativity 1 ++ --
C – Operator 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 ·
🌐
Cppreference
en.cppreference.com › w › cpp › language › operator_precedence.html
C++ Operator Precedence - cppreference.com
December 24, 2024 - When parsing an expression, an ... << a) & b and *(p++), and not as std::cout << (a & b) or (*p)++. Operators that have the same precedence are bound to their arguments in the direction of their associativity....
Top answer
1 of 3
7

The precedence should be the same as it is in C, for several reasons.

C got it right

C got the relative precedences for these operators right*, so there's nothing to fix by changing it:

  • ~ has the highest precedence, by analogy with logical operators. It's also very common to use ~ to invert a mask that you want to apply with &, so ~ should have higher precedence than &.
  • Shifts have the next-highest because usually one or both sides are literals or other fixed constants, i.e. things which require no bitwise operations to compute. Shifts are also analogous to exponentiation (1 << n equals 2 ** n) which has a high precedence in arithmetic.
  • & has lower precedence than shifts because often you want to shift something to the correct position, and then use & to keep just some of the bits, like x >> offset & mask. If the mask is something like 0xFF then you can clearly see that the result will only have the lowest 8 bits; this is more readable than (x & mask) >> offset because the mask no longer shows which bits might be set in the result.
  • ^ and | have lower precedence than & both by analogy to logical operators, and because it's common to use them to combine the results of multiple shifts or masked-shifts. The relative precedence of ^ and | doesn't have much practical importance because there are few reasons to use them together.

*By "right", I mean C avoids requiring parentheses as much as possible for the most common uses of bitwise operators, and also matches the relative precedences of analogous operators in mathematics. Of course there's some room for debate over which uses are most common and which mathematical operators are more analogous, but no design rationale can be perfectly objective.

Compatibility with C is important

There are a lot of clever things you can do with bitwise operators, but most programmers will not invent their own new clever things. If people use bitwise operators, it's mostly in code they copy-paste from Stack Overflow, or popular articles such as this one.

These sources will invariably assume C's precedence rules for bitwise operators, either because they are written in C, or because they are written in other languages with the same precedence rules for bitwise operators (and that's every other popular language with bitwise operators, as far as I know).

Therefore, your users' interests are best served by not adding an extra step that must be taken when translating such code into your language, particularly an extra step which users don't necessarily know they need to take.

Users won't learn the precedences

For bitwise operators (and sometimes even boolean logical operators!) it's often recommended to use parentheses to clarify the order of operations, even when parentheses would be redundant. Because even if you know the bitwise operator precedences, most of the people who will read your code do not.

The vast majority of programmers do not use these operators very often, and will not spend the effort to memorise their precedences. (This is especially the case for the relative precedences of bitwise and non-bitwise operators, like << and +.) So, if people will use brackets anyway, you don't gain anything by giving them different precedences. Put another way, this isn't an efficient use of your language's strangeness budget.

2 of 3
2

If you want to minimize the need for parentheses

Let's typical use case of bitshift/masking operations.

Bit arrays

Suppose that you have a bit array class, allowing an integer (or array of integers) to be treated as an array of bits. It provides the operations:

  • get bitarray[i]if ((backing_int & (1 << i)) != 0)
  • set bitarrary[i] = valuebacking_int = (backing_int & ~(1 << i)) | (value << i)

To make these work without parentheses, you would need the precedence order:

  1. Shifts (<< and >>)
  2. NOT (~)
  3. AND (&)
  4. OR (|)
  5. Relationals (==, !=, etc.)

Note that I only used left shift (<<), not right shift (>>), but since these are parallel and inverse operators, similar to + and -, it would be weird to not have them at the same precedence level.

Also, I didn't include XOR (^) in my example. If you think of XOR as being a one-bit addition (with the carry bit ignored), then it makes sense to place it below AND (which is a one-bit multiplication). If you think of XOR as being a Boolean != operator, it makes sense to treat them at the same level as relationals — if you put relationals below OR, as I have done.

UTF-8 decoding

The following line of C(++) code decodes a 4-byte UTF-8 sequence to the corresponding Unicode code point (after the validity check has been done):

code_point = ((bytes[0] & 7) << 18) | ((bytes[1] & 0x3F) << 12) | ((bytes[2] & 0x3F) << 6) | (bytes[3] & 0x3F);

You could make the parentheses unnecessary by using the precedence order:

  1. AND (&)
  2. Shifts (<<, >>)
  3. OR (|)

Note that this switches the relative precedence of AND and shifting compared to the previous example.

If you want to minimize confusion

As @kaya3 pointed out, if you use the C syntax for operators, people will expect them to work the same as in C. Just as if you allow C-style ;, people will expect statement termination semantics to be the same as in C — to the confusion of JavaScript developers.

So, if you're committed to the symbols <<, >>, ~, &, |. and ^, it would probably be a good idea to keep C's precedence hierarchy for them.

If you decide to spell the operators differently (e.g., the word-like lshift, rshift, not, and, or, and xor), then this argument might not apply as much.

🌐
Reddit
reddit.com › r/programminglanguages › why are the usual operator precedence rules are the way they are?
r/ProgrammingLanguages on Reddit: Why are the usual operator precedence rules are the way they are?
October 19, 2022 -

For instance:

  • Unary plus/minus having higher precedence than binary plus/minus and even multiplication/division. I think it'd make sense for them to have the same precedence as their binary counterparts.

  • not / ! > and / && > or / ||. Why not the same precedence?

I checked C++, Java, JavaScript and Python and these rules apply there, and I assume in other major languages. I assume there's logic behind these and other precedence rules but I am not sure what that is. I am implementing my own programming language and I want to understand why the rules are the way they are, rather than just blindly following them

🌐
Reddit
reddit.com › r/c_programming › precedence of logical operators and parantheses
r/C_Programming on Reddit: Precedence of logical operators and parantheses
March 6, 2023 -

Hi,

I have a question about how this expression is evaluated in C:

x != 0 && (20 / x) < 5 

I know that logical operators have higher precedence than that of relational or assignment operators.

But what about the parentheses? I mean, why isn't (20 / x) evaluated first?

UPDATE: In fact, logical operators have lower precedence than that of relational operators.

Top answer
1 of 5
22
C has short-circuiting logical operators, so the left hand of the && will be fully evaluated before the right. If the left part results in a false, the right hand won't be evaluated at all.
2 of 5
8
Precedence and associativity determine the syntax and structure of expressions, but order of evaluation and sequence points are semantic rules applied to the compiler’s model of the source code, and they place no limits whatsoever on the syntax as long as the necessary operations are supported (and ofc the parse actually succeeds). E.g., if we start with source expression(-statement) a = b + c * d; and without any precedence rules, we might parse this to a(n abstract syntax) tree _+_ / \ = * / \ / \ a b c d (corresponding to (a = b) + (c * d)) or * / \ = d / \ a + / \ b c (corr. to (a = (b + c)) * d). But with precedence, the only valid AST is = / \ a + / \ b * / \ c d (a = (b + (c * d))) because = has the lowest precedence here, then +, then *. Low precedence = low priority for selection of binding, so anything with higher precedence wins. If we start with a = b = c = d + e + f + g; then without associativity rules, we might obtain _=_ / \ = = / \ / \ a b c _+_ / \ + + / \ / \ d e f g corresponding to expression (a = b) = (c = (d + e) + (f + g)); With associativity rules, = only binds to the right, and + only binds to the left: = / \ a = / \ b = / \ c + / \ + g / \ + f / \ d e corresponding to a = (b = (c = (((d + e) + f) + g))); At no point has any of this touched on the actual process of executing the tree, and that’s where order of evaluation (i.e., executing an expression to obtain a result value) comes in. a + b + c + d might be executed as register typeof(a+b+c+d) t; t = a + b; t += c; t += d; or t = b + c; t += a; t += d; or t = a + c; t += d; t += b; etc. because + imposes no sequencing on its operands. And if there aren’t side-effects in a/b/c, the compiler might even generate code for something like a + b + c + d as two subexpressions a + b and c + d executed in parallel on different threads or via SIMD trickery, then merge-/horizontal-summed, giving us roughly inline static void unord(int x, ...) {(void)x;} #define unord(...)unord(0,__VA_ARGS__) unord(s = a + b, t = c + d); s += t; Sequence points (e.g., binary , && || operators; declarator separator ,; statement separation ; { }; function entry/exit) are required anywhere a specific order of evaluation is needed, whether or not it lines up with the lexical (source-code) ordering. When in doubt, break it up into multiple statements (as in the s/t examples above) so it’s clear what you want to happen.