Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > AND and OR and parentheses

Reply
Thread Tools

AND and OR and parentheses

 
 
Richard Bos
Guest
Posts: n/a
 
      08-19-2005
Netocrat <(E-Mail Removed)> wrote:

> On Wed, 17 Aug 2005 06:38:50 +0000, Richard Bos wrote:
>
> > "Malcolm" <(E-Mail Removed)> wrote:
> >
> >> The rule I use is that you can rely on your reader to know that * and / have
> >> a higher precedence than + and -, and everything else needs parentheses.

> >
> > Are you sure?
> >
> > #include <stdio.h>
> >
> > int main(void)
> > {
> > int i, j;
> >
> > for ((i=1); (i<10); (i++)) {
> > (j=(i*10+3));
> > (printf(("Value nr. %d is %d\n"), (i), (j)));
> > }
> > }

>
> Well if we're going to take Malcolm's statement literally without applying
> common sense,


My point was that, as he phrased it, Malcolm's statement doesn't really
allow common sense. Yes, my example of putting parens around a whole
assignment statement is a bit over the top; but I doubt that, in writing
"everything else", Malcolm had thought about = , ?: sizeof and so forth.
In fact, I'd say that he thought about | & ^ || && == != < > and that's
probably about it.

Richard
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      08-20-2005

"Richard Bos" <(E-Mail Removed)> wrote
>> >> The rule I use is that you can rely on your reader to know that * and
>> >> / have
>> >> a higher precedence than + and -, and everything else needs
>> >> parentheses.
>> >

> My point was that, as he phrased it, Malcolm's statement doesn't really
> allow common sense. Yes, my example of putting parens around a whole
> assignment statement is a bit over the top; but I doubt that, in writing
> "everything else", Malcolm had thought about = , ?: sizeof and so forth.
> In fact, I'd say that he thought about | & ^ || && == != < > and that's
> probably about it.
>

The other rule, which is just as important, is that the reader can't be
expected to cope with more than three layers of nesting. Unfortunately this
makes the "parenthese round everything ambiguous" rule hard to follow.

Personally I always give parenthese to sizeof() to make it look like a
function call, but I don't obvious write (sizeof(x)) which a literal
interpretation would demand.

The ++ operator is quirky because precedence and order of evaluation is the
same. It is so idiomatic that you have to use it in compound expressions,
though I prefer to put it on a line of its own where possible.


 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-20-2005
Malcolm wrote:

> Personally I always give parenthese to sizeof()
> to make it look like a function call,


I avoid giving parentheses to sizeof,
because I don't want a constant expression
to look like a function call.

--
pete
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      08-20-2005
Malcolm wrote:
> "Richard Bos" <(E-Mail Removed)> wrote
>
>>>>>The rule I use is that you can rely on your reader to know that * and
>>>>>/ have
>>>>>a higher precedence than + and -, and everything else needs
>>>>>parentheses.
>>>>

>>My point was that, as he phrased it, Malcolm's statement doesn't really
>>allow common sense. Yes, my example of putting parens around a whole
>>assignment statement is a bit over the top; but I doubt that, in writing
>>"everything else", Malcolm had thought about = , ?: sizeof and so forth.
>>In fact, I'd say that he thought about | & ^ || && == != < > and that's
>>probably about it.
>>

>
> The other rule, which is just as important, is that the reader can't be
> expected to cope with more than three layers of nesting. Unfortunately this
> makes the "parenthese round everything ambiguous" rule hard to follow.
>

Who made this rule? There is nothing magic about three nesting levels.

> Personally I always give parenthese to sizeof() to make it look like a
> function call, but I don't obvious write (sizeof(x)) which a literal
> interpretation would demand.
>

Why? It is not a function. If the argument needs parentheses I insert a
space 'sizeof (int)' so that it doesn't look like a function.

> The ++ operator is quirky because precedence and order of evaluation is the
> same. It is so idiomatic that you have to use it in compound expressions,
> though I prefer to put it on a line of its own where possible.
>

What is 'quirky' about it? Sharing precedence level with sizeof is a
problem how?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parentheses and newlines jsnark Ruby 2 02-18-2012 07:56 PM
Parentheses and compiler optimzation spasmous C Programming 15 04-13-2008 03:35 AM
Eliminating Parentheses and Words Between Them In Word Doc Martin Computer Support 9 07-13-2007 07:06 AM
C parentheses, brackets and brace case.learning@gmail.com C Programming 8 06-16-2007 12:41 AM
Substitution and Text Within Parentheses Addy Perl Misc 2 08-26-2003 01:15 PM



Advertisments