Path: utzoo!attcan!uunet!mcsun!sunic!dkuug!iesd!iesd!fischer From: fischer@iesd.auc.dk (Lars P. Fischer) Newsgroups: comp.lang.c Subject: Re: This one bit me today Message-ID: Date: 7 Oct 89 20:36:17 GMT References: <2432@hub.UUCP> <832@crdos1.crd.ge.COM> Sender: news@iesd.auc.dk (UseNet News) Organization: Mathematics and Computer Science, University of Aalborg Lines: 38 In-reply-to: davidsen@crdos1.crd.ge.COM's message of 5 Oct 89 15:17:54 GMT In article <832@crdos1.crd.ge.COM> davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes: > It pops up in macros from time to time. Consider: > #define xavg(m) (sum+3)/m From K&R, 2.ed, p.90: Some care also has to be taken with parentheses to make sure the order of evaluation is preserved. From Harbison & Steele, 2.ed., p. 36: As a rule, it is safest always to parenthesize each parameter appearing in the macro body. The entire body, if it is syntactically an expression, should also be parenthesized. (The whole page is used by section 3.3.6 "Precedence errors in macro expansion"). So, what do we learn. Well, the above should have been: #define xavg(m) ((sum+3) / (m)) Hopefully, we also learn that any C programmer worth his salt knows K&R by heart, and has H&S handy :-). > It's even worse if someone makes a mistake like this in a system >header file... If you don't know how to use CPP you shouldn't be allowed to write system header files. /Lars -- Copyright 1989 Lars Fischer; you can redistribute only if your recipients can. Lars Fischer, fischer@iesd.auc.dk, {...}!mcvax!iesd!fischer Department of Computer Science, University of Aalborg, DENMARK. "That makes 100 errors; please try again" --TeX