📄 operator-precedence-problems.html
字号:
<html lang="en">
<head>
<title>The C Preprocessor</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The C Preprocessor">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
1997, 1998, 1999, 2000, 2001, 2002, 2003
Free Software Foundation, Inc.
<p>Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1 or
any later version published by the Free Software Foundation. A copy of
the license is included in the
section entitled "GNU Free Documentation License".
<p>This manual contains no Invariant Sections. The Front-Cover Texts are
(a) (see below), and the Back-Cover Texts are (b) (see below).
<p>(a) The FSF's Front-Cover Text is:
<p>A GNU Manual
<p>(b) The FSF's Back-Cover Text is:
<p>You have freedom to copy and modify this GNU Manual, like GNU
software. Copies published by the Free Software Foundation raise
funds for GNU development.
-->
</head>
<body>
<div class="node">
<p>
Node:<a name="Operator%20Precedence%20Problems">Operator Precedence Problems</a>,
Next:<a rel="next" accesskey="n" href="Swallowing-the-Semicolon.html#Swallowing%20the%20Semicolon">Swallowing the Semicolon</a>,
Previous:<a rel="previous" accesskey="p" href="Misnesting.html#Misnesting">Misnesting</a>,
Up:<a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro%20Pitfalls">Macro Pitfalls</a>
<hr><br>
</div>
<h4 class="subsection">Operator Precedence Problems</h4>
<p>You may have noticed that in most of the macro definition examples shown
above, each occurrence of a macro argument name had parentheses around
it. In addition, another pair of parentheses usually surround the
entire macro definition. Here is why it is best to write macros that
way.
<p>Suppose you define a macro as follows,
<pre class="example"> #define ceil_div(x, y) (x + y - 1) / y
</pre>
<p>whose purpose is to divide, rounding up. (One use for this operation is
to compute how many <code>int</code> objects are needed to hold a certain
number of <code>char</code> objects.) Then suppose it is used as follows:
<pre class="example"> a = ceil_div (b & c, sizeof (int));
==> a = (b & c + sizeof (int) - 1) / sizeof (int);
</pre>
<p>This does not do what is intended. The operator-precedence rules of
C make it equivalent to this:
<pre class="example"> a = (b & (c + sizeof (int) - 1)) / sizeof (int);
</pre>
<p>What we want is this:
<pre class="example"> a = ((b & c) + sizeof (int) - 1)) / sizeof (int);
</pre>
<p>Defining the macro as
<pre class="example"> #define ceil_div(x, y) ((x) + (y) - 1) / (y)
</pre>
<p>provides the desired result.
<p>Unintended grouping can result in another way. Consider <code>sizeof
ceil_div(1, 2)</code>. That has the appearance of a C expression that would
compute the size of the type of <code>ceil_div (1, 2)</code>, but in fact it
means something very different. Here is what it expands to:
<pre class="example"> sizeof ((1) + (2) - 1) / (2)
</pre>
<p>This would take the size of an integer and divide it by two. The
precedence rules have put the division outside the <code>sizeof</code> when it
was intended to be inside.
<p>Parentheses around the entire macro definition prevent such problems.
Here, then, is the recommended way to define <code>ceil_div</code>:
<pre class="example"> #define ceil_div(x, y) (((x) + (y) - 1) / (y))
</pre>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -