source: public/doc/gnu-c/Binary-Operator-Grammar.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.7 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
71on the left and one on the right.
72</p>
73<p>All the binary operators in C are syntactically left-associative.
74This means that <code>a&nbsp;<var>op</var>&nbsp;b&nbsp;<var>op</var>&nbsp;c</code><!-- /@w --> means <code>(a&nbsp;<var>op</var>&nbsp;b)&nbsp;<var>op</var>&nbsp;c</code><!-- /@w -->. However, you should only write repeated
75operators without parentheses using &lsquo;<samp>+</samp>&rsquo;, &lsquo;<samp>-</samp>&rsquo;, &lsquo;<samp>*</samp>&rsquo; and
76&lsquo;<samp>/</samp>&rsquo;, because those cases are clear from algebra. So it is ok to
77write <code>a + b + c</code> or <code>a - b - c</code>, but never <code>a == b ==
78c</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
81grammatical order of the various operators. The operators with the
82highest precedence grab adjoining operands first; these expressions
83then become operands for operators of lower precedence.
84</p>
85<p>The precedence order of operators in C is fully specified, so any
86combination of operations leads to a well-defined nesting. We state
87only part of the full precedence ordering here because it is bad
88practice for C code to depend on the other cases. For cases not
89specified in this chapter, always use parentheses to make the nesting
90explicit.<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 (&lsquo;<samp>.</samp>&rsquo; and &lsquo;<samp>-&gt;</samp>&rsquo;).
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&mdash;but watch out!
107
108</li><li> Logical operators &lsquo;<samp>&amp;&amp;</samp>&rsquo; and &lsquo;<samp>||</samp>&rsquo;&mdash;but watch out!
109
110</li><li> Conditional expression with &lsquo;<samp>?</samp>&rsquo; and &lsquo;<samp>:</samp>&rsquo;.
111
112</li><li> Assignments.
113
114</li><li> Sequential execution (the comma operator, &lsquo;<samp>,</samp>&rsquo;).
115</li></ol>
116
117<p>Two of the lines in the above list say &ldquo;but watch out!&rdquo; That means
118that the line covers operators with subtly different precedence.
119Never depend on the grammar of C to decide how two comparisons nest;
120instead, always use parentheses to specify their nesting.
121</p>
122<p>You can let several &lsquo;<samp>&amp;&amp;</samp>&rsquo; operators associate, or several
123&lsquo;<samp>||</samp>&rsquo; operators, but always use parentheses to show how &lsquo;<samp>&amp;&amp;</samp>&rsquo;
124and &lsquo;<samp>||</samp>&rsquo; 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&mdash;but watch out!
132
133</li><li> Conditional expression with &lsquo;<samp>?</samp>&rsquo; and &lsquo;<samp>:</samp>&rsquo;.
134</li></ol>
135
136<p>The caveat for bitwise and shift operators is like that for logical
137operators: you can let multiple uses of one bitwise operator
138associate, but always use parentheses to control nesting of dissimilar
139operators.
140</p>
141<p>These lists do not specify any precedence ordering between the bitwise
142and shift operators of the second list and the binary operators above
143conditional expressions in the first list. When they come together,
144parenthesize 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
152remembering anything about the C precedence order beyond what&rsquo;s stated
153here. I studied the full precedence table to write the parser, and
154promptly forgot it again. If you need to look up the full precedence order
155to understand some C code, fix the code with parentheses so nobody else
156needs to do that.</p>
157</div>
158<hr>
159<div class="header">
160<p>
161Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.