Path: utzoo!attcan!uunet!husc6!cs.utexas.edu!oakhill!steve From: steve@oakhill.UUCP (steve) Newsgroups: comp.lang.c Subject: Re: Unnecessary Parenthesis Summary: best way to use parenthesis in macros (somewhat overkill) Keywords: Use 'em all the time in macros... Message-ID: <1401@devsys.oakhill.UUCP> Date: 25 Jul 88 19:07:25 GMT References: <2089@ssc-vax.UUCP> <441@kaon.uchicago.edu> Organization: Motorola Inc. Austin, Tx Lines: 172 I'm sorry I can't include the text I'm mentioning since I'm haveing net reply problems, but I'll fake it as best I can. In an absolute sense > #define square(x) (x * x) shouldn't be : > #define square(x) ((x) * (x)) it should be : > #define square(x) (temp = (x),(temp * temp)) in order to avoid problems with sending in a post incremented variable; BUT, - as with everything else in C, either of the first two forms are good if you're careful and know what you're doing. Form 1 works if you know you're sending in a simple variable. Form 2 works if you are not sending in an expression that changes the variable. Form 3 is slower than the other two, but more conservative. Use your best judgement - macros are dangerous thing if not used carefully That is why C compilers have a -P option. Your mooncalf - Steve To: paul@zaphod.UChicago.Edu Subject: Re: Unnecessary Parenthesis Newsgroups: comp.lang.c In-Reply-To: <441@kaon.uchicago.edu> References: <2089@ssc-vax.UUCP> Organization: Motorola Inc., Austin Tx. Cc: Bcc: I'm sorry I can't include the text I'm mentioning but > #define square(x) (x * x) shouldn't be : > #define square(x) ((x) * (x)) it should be : > #define square(x) (temp = (x),(temp * temp)) in order to avoid problems with sending in a post incremented variable; BUT, - as with everything else in C, either of the first two forms are good if you're careful and know what you're doing. Form 1 works if you know you're sending in a simple variable. Form 2 works if you are not sending in an expression that changes the variable. Form 3 is slower than the other two, but more conservative. Your mooncalf - Steve From steve Mon Jul 25 13:54:49 1988 Return-Path: Received: by devsys.com (3.2/SMI-3.2) id AA13794; Mon, 25 Jul 88 13:54:49 CDT Date: Mon, 25 Jul 88 13:54:49 CDT From: steve Message-Id: <8807251854.AA13794@devsys.com> To: paul@zaphod.UChicago.Edu Subject: Re: Unnecessary Parenthesis Newsgroups: comp.lang.c In-Reply-To: <441@kaon.uchicago.edu> References: <2089@ssc-vax.UUCP> Organization: Motorola Inc., Austin Tx. Cc: I'm sorry I can't include the text I'm mentioning but having sending problems. I'l try to fake it. > #define square(x) (x * x) shouldn't be : > #define square(x) ((x) * (x)) it should be : > #define square(x) (temp = (x),(temp * temp)) in order to avoid problems with sending in a post incremented variable; BUT, - as with everything else in C, either of the first two forms are good if you're careful and know what you're doing. Form 1 works if you know you're sending in a simple variable. Form 2 works if you are not sending in an expression that changes the variable. Form 3 is slower than the other two, but more conservative. Your mooncalf - Steve To: paul@zaphod.UChicago.Edu Subject: Re: Unnecessary Parenthesis Newsgroups: comp.lang.c In-Reply-To: <441@kaon.uchicago.edu> References: <2089@ssc-vax.UUCP> Organization: Motorola Inc., Austin Tx. Cc: Bcc: I'm sorry I can't include the text I'm mentioning but > #define square(x) (x * x) shouldn't be : > #define square(x) ((x) * (x)) it should be : > #define square(x) (temp = (x),(temp * temp)) in order to avoid problems with sending in a post incremented variable; BUT, - as with everything else in C, either of the first two forms are good if you're careful and know what you're doing. Form 1 works if you know you're sending in a simple variable. Form 2 works if you are not sending in an expression that changes the variable. Form 3 is slower than the other two, but more conservative. Your mooncalf - Steve To: paul@zaphod.UChicago.Edu Subject: Re: Unnecessary Parenthesis Newsgroups: comp.lang.c In-Reply-To: <441@kaon.uchicago.edu> References: <2089@ssc-vax.UUCP> Organization: Motorola Inc., Austin Tx. Cc: Bcc: I'm sorry I can't include the text I'm mentioning but having sending problems. I'l try to fake it. > #define square(x) (x * x) shouldn't be : > #define square(x) ((x) * (x)) it should be : > #define square(x) (temp = (x),(temp * temp)) in order to avoid problems with sending in a post incremented variable; BUT, - as with everything else in C, either of the first two forms are good if you're careful and know what you're doing. Form 1 works if you know you're sending in a simple variable. Form 2 works if you are not sending in an expression that changes the variable. Form 3 is slower than the other two, but more conservative. Your mooncalf - Steve > Nothing I say is quoteable - so don't expect to see anything important here.