source: public/doc/gnu-c/Operator-Precedence-Problems.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: 5.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>Operator Precedence Problems (GNU C Language Manual)</title>
23
24<meta name="description" content="Operator Precedence Problems (GNU C Language Manual)">
25<meta name="keywords" content="Operator Precedence Problems (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="Macro-Pitfalls.html" rel="up" title="Macro Pitfalls">
33<link href="Swallowing-the-Semicolon.html" rel="next" title="Swallowing the Semicolon">
34<link href="Misnesting.html" rel="prev" title="Misnesting">
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="Operator-Precedence-Problems"></span><div class="header">
59<p>
60Next: <a href="Swallowing-the-Semicolon.html" accesskey="n" rel="next">Swallowing the Semicolon</a>, Previous: <a href="Misnesting.html" accesskey="p" rel="prev">Misnesting</a>, Up: <a href="Macro-Pitfalls.html" accesskey="u" rel="up">Macro Pitfalls</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="Operator-Precedence-Problems-1"></span><h4 class="subsubsection">26.5.10.2 Operator Precedence Problems</h4>
64<span id="index-parentheses-in-macro-bodies"></span>
65
66<p>You may have noticed that in most of the macro definition examples shown
67above, each occurrence of a macro parameter name had parentheses around
68it. In addition, another pair of parentheses usually surrounds the
69entire macro definition. Here is why it is best to write macros that
70way.
71</p>
72<p>Suppose you define a macro as follows,
73</p>
74<div class="example">
75<pre class="example">#define ceil_div(x, y) (x + y - 1) / y
76</pre></div>
77
78<p>whose purpose is to divide, rounding up. (One use for this operation is
79to compute how many <code>int</code> objects are needed to hold a certain
80number of <code>char</code> objects.) Then suppose it is used as follows:
81</p>
82<div class="example">
83<pre class="example">a = ceil_div (b &amp; c, sizeof (int));
84 &rarr; a = (b &amp; c + sizeof (int) - 1) / sizeof (int);
85</pre></div>
86
87<p>This does not do what is intended. The operator-precedence rules of
88C make it equivalent to this:
89</p>
90<div class="example">
91<pre class="example">a = (b &amp; (c + sizeof (int) - 1)) / sizeof (int);
92</pre></div>
93
94<p>What we want is this:
95</p>
96<div class="example">
97<pre class="example">a = ((b &amp; c) + sizeof (int) - 1)) / sizeof (int);
98</pre></div>
99
100<p>Defining the macro as
101</p>
102<div class="example">
103<pre class="example">#define ceil_div(x, y) ((x) + (y) - 1) / (y)
104</pre></div>
105
106<p>provides the desired result.
107</p>
108<p>Unintended grouping can result in another way. Consider <code>sizeof
109ceil_div(1, 2)</code>. That has the appearance of a C expression that would
110compute the size of the type of <code>ceil_div (1, 2)</code>, but in fact it
111means something very different. Here is what it expands to:
112</p>
113<div class="example">
114<pre class="example">sizeof ((1) + (2) - 1) / (2)
115</pre></div>
116
117<p>This would take the size of an integer and divide it by two. The
118precedence rules have put the division outside the <code>sizeof</code> when it
119was intended to be inside.
120</p>
121<p>Parentheses around the entire macro definition prevent such problems.
122Here, then, is the recommended way to define <code>ceil_div</code>:
123</p>
124<div class="example">
125<pre class="example">#define ceil_div(x, y) (((x) + (y) - 1) / (y))
126</pre></div>
127
128<hr>
129<div class="header">
130<p>
131Next: <a href="Swallowing-the-Semicolon.html" accesskey="n" rel="next">Swallowing the Semicolon</a>, Previous: <a href="Misnesting.html" accesskey="p" rel="prev">Misnesting</a>, Up: <a href="Macro-Pitfalls.html" accesskey="u" rel="up">Macro Pitfalls</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>
132</div>
133
134
135
136</body>
137</html>
Note: See TracBrowser for help on using the repository browser.