1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Binary Operator Grammar (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Binary Operator Grammar (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Binary Operator Grammar (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="index.html" rel="up" title="Top">
|
---|
33 | <link href="Order-of-Execution.html" rel="next" title="Order of Execution">
|
---|
34 | <link href="Avoid-Comma.html" rel="prev" title="Avoid Comma">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Binary-Operator-Grammar"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Order-of-Execution.html" accesskey="n" rel="next">Order of Execution</a>, Previous: <a href="Execution-Control-Expressions.html" accesskey="p" rel="prev">Execution Control Expressions</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Binary-Operator-Grammar-1"></span><h2 class="chapter">9 Binary Operator Grammar</h2>
|
---|
64 | <span id="index-binary-operator-grammar"></span>
|
---|
65 | <span id="index-grammar_002c-binary-operator"></span>
|
---|
66 | <span id="index-operator-precedence"></span>
|
---|
67 | <span id="index-precedence_002c-operator"></span>
|
---|
68 | <span id="index-left_002dassociative"></span>
|
---|
69 |
|
---|
70 | <p><em>Binary operators</em> are those that take two operands, one
|
---|
71 | on the left and one on the right.
|
---|
72 | </p>
|
---|
73 | <p>All the binary operators in C are syntactically left-associative.
|
---|
74 | This means that <code>a <var>op</var> b <var>op</var> c</code><!-- /@w --> means <code>(a <var>op</var> b) <var>op</var> c</code><!-- /@w -->. However, you should only write repeated
|
---|
75 | operators without parentheses using ‘<samp>+</samp>’, ‘<samp>-</samp>’, ‘<samp>*</samp>’ and
|
---|
76 | ‘<samp>/</samp>’, because those cases are clear from algebra. So it is ok to
|
---|
77 | write <code>a + b + c</code> or <code>a - b - c</code>, but never <code>a == b ==
|
---|
78 | c</code> or <code>a % b % c</code>.
|
---|
79 | </p>
|
---|
80 | <p>Each C operator has a <em>precedence</em>, which is its rank in the
|
---|
81 | grammatical order of the various operators. The operators with the
|
---|
82 | highest precedence grab adjoining operands first; these expressions
|
---|
83 | then become operands for operators of lower precedence.
|
---|
84 | </p>
|
---|
85 | <p>The precedence order of operators in C is fully specified, so any
|
---|
86 | combination of operations leads to a well-defined nesting. We state
|
---|
87 | only part of the full precedence ordering here because it is bad
|
---|
88 | practice for C code to depend on the other cases. For cases not
|
---|
89 | specified in this chapter, always use parentheses to make the nesting
|
---|
90 | explicit.<a id="DOCF2" href="#FOOT2"><sup>2</sup></a>
|
---|
91 | </p>
|
---|
92 | <p>You can depend on this subsequence of the precedence ordering
|
---|
93 | (stated from highest precedence to lowest):
|
---|
94 | </p>
|
---|
95 | <ol>
|
---|
96 | <li> Component access (‘<samp>.</samp>’ and ‘<samp>-></samp>’).
|
---|
97 |
|
---|
98 | </li><li> Unary prefix operators.
|
---|
99 |
|
---|
100 | </li><li> Unary postfix operators.
|
---|
101 |
|
---|
102 | </li><li> Multiplication, division, and remainder (they have the same precedence).
|
---|
103 |
|
---|
104 | </li><li> Addition and subtraction (they have the same precedence).
|
---|
105 |
|
---|
106 | </li><li> Comparisons—but watch out!
|
---|
107 |
|
---|
108 | </li><li> Logical operators ‘<samp>&&</samp>’ and ‘<samp>||</samp>’—but watch out!
|
---|
109 |
|
---|
110 | </li><li> Conditional expression with ‘<samp>?</samp>’ and ‘<samp>:</samp>’.
|
---|
111 |
|
---|
112 | </li><li> Assignments.
|
---|
113 |
|
---|
114 | </li><li> Sequential execution (the comma operator, ‘<samp>,</samp>’).
|
---|
115 | </li></ol>
|
---|
116 |
|
---|
117 | <p>Two of the lines in the above list say “but watch out!” That means
|
---|
118 | that the line covers operators with subtly different precedence.
|
---|
119 | Never depend on the grammar of C to decide how two comparisons nest;
|
---|
120 | instead, always use parentheses to specify their nesting.
|
---|
121 | </p>
|
---|
122 | <p>You can let several ‘<samp>&&</samp>’ operators associate, or several
|
---|
123 | ‘<samp>||</samp>’ operators, but always use parentheses to show how ‘<samp>&&</samp>’
|
---|
124 | and ‘<samp>||</samp>’ nest with each other. See <a href="Logical-Operators.html">Logical Operators</a>.
|
---|
125 | </p>
|
---|
126 | <p>There is one other precedence ordering that code can depend on:
|
---|
127 | </p>
|
---|
128 | <ol>
|
---|
129 | <li> Unary postfix operators.
|
---|
130 |
|
---|
131 | </li><li> Bitwise and shift operators—but watch out!
|
---|
132 |
|
---|
133 | </li><li> Conditional expression with ‘<samp>?</samp>’ and ‘<samp>:</samp>’.
|
---|
134 | </li></ol>
|
---|
135 |
|
---|
136 | <p>The caveat for bitwise and shift operators is like that for logical
|
---|
137 | operators: you can let multiple uses of one bitwise operator
|
---|
138 | associate, but always use parentheses to control nesting of dissimilar
|
---|
139 | operators.
|
---|
140 | </p>
|
---|
141 | <p>These lists do not specify any precedence ordering between the bitwise
|
---|
142 | and shift operators of the second list and the binary operators above
|
---|
143 | conditional expressions in the first list. When they come together,
|
---|
144 | parenthesize them. See <a href="Bitwise-Operations.html">Bitwise Operations</a>.
|
---|
145 | </p>
|
---|
146 | <div class="footnote">
|
---|
147 | <hr>
|
---|
148 | <h4 class="footnotes-heading">Footnotes</h4>
|
---|
149 |
|
---|
150 | <h5><a id="FOOT2" href="#DOCF2">(2)</a></h3>
|
---|
151 | <p>Personal note from Richard Stallman: I wrote GCC without
|
---|
152 | remembering anything about the C precedence order beyond what’s stated
|
---|
153 | here. I studied the full precedence table to write the parser, and
|
---|
154 | promptly forgot it again. If you need to look up the full precedence order
|
---|
155 | to understand some C code, fix the code with parentheses so nobody else
|
---|
156 | needs to do that.</p>
|
---|
157 | </div>
|
---|
158 | <hr>
|
---|
159 | <div class="header">
|
---|
160 | <p>
|
---|
161 | Next: <a href="Order-of-Execution.html" accesskey="n" rel="next">Order of Execution</a>, Previous: <a href="Execution-Control-Expressions.html" accesskey="p" rel="prev">Execution Control Expressions</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
162 | </div>
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | </body>
|
---|
167 | </html>
|
---|