1<html lang="en"> 2<head> 3<title>Operator Precedence Problems - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Macro-Pitfalls.html#Macro-Pitfalls" title="Macro Pitfalls"> 9<link rel="prev" href="Misnesting.html#Misnesting" title="Misnesting"> 10<link rel="next" href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon" title="Swallowing the Semicolon"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Operator-Precedence-Problems"></a> 54<p> 55Next: <a rel="next" accesskey="n" href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon">Swallowing the Semicolon</a>, 56Previous: <a rel="previous" accesskey="p" href="Misnesting.html#Misnesting">Misnesting</a>, 57Up: <a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a> 58<hr> 59</div> 60 61<h4 class="subsection">3.10.2 Operator Precedence Problems</h4> 62 63<p><a name="index-parentheses-in-macro-bodies-74"></a> 64You may have noticed that in most of the macro definition examples shown 65above, each occurrence of a macro argument name had parentheses around 66it. In addition, another pair of parentheses usually surround the 67entire macro definition. Here is why it is best to write macros that 68way. 69 70 <p>Suppose you define a macro as follows, 71 72<pre class="smallexample"> #define ceil_div(x, y) (x + y - 1) / y 73</pre> 74 <p class="noindent">whose purpose is to divide, rounding up. (One use for this operation is 75to compute how many <code>int</code> objects are needed to hold a certain 76number of <code>char</code> objects.) Then suppose it is used as follows: 77 78<pre class="smallexample"> a = ceil_div (b & c, sizeof (int)); 79 ==> a = (b & c + sizeof (int) - 1) / sizeof (int); 80</pre> 81 <p class="noindent">This does not do what is intended. The operator-precedence rules of 82C make it equivalent to this: 83 84<pre class="smallexample"> a = (b & (c + sizeof (int) - 1)) / sizeof (int); 85</pre> 86 <p class="noindent">What we want is this: 87 88<pre class="smallexample"> a = ((b & c) + sizeof (int) - 1)) / sizeof (int); 89</pre> 90 <p class="noindent">Defining the macro as 91 92<pre class="smallexample"> #define ceil_div(x, y) ((x) + (y) - 1) / (y) 93</pre> 94 <p class="noindent">provides the desired result. 95 96 <p>Unintended grouping can result in another way. Consider <code>sizeof 97ceil_div(1, 2)</code>. That has the appearance of a C expression that would 98compute the size of the type of <code>ceil_div (1, 2)</code>, but in fact it 99means something very different. Here is what it expands to: 100 101<pre class="smallexample"> sizeof ((1) + (2) - 1) / (2) 102</pre> 103 <p class="noindent">This would take the size of an integer and divide it by two. The 104precedence rules have put the division outside the <code>sizeof</code> when it 105was intended to be inside. 106 107 <p>Parentheses around the entire macro definition prevent such problems. 108Here, then, is the recommended way to define <code>ceil_div</code>: 109 110<pre class="smallexample"> #define ceil_div(x, y) (((x) + (y) - 1) / (y)) 111</pre> 112 </body></html> 113 114