Remebering the order of precedence

K

karthikbalaguru

Hi,

Is there a technique to easily remember the order of precedence
of all C operators from high to low priority ?

I think this will save time rather than referring to the chart
for every expression.
Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

Hi,

Is there a technique to easily remember the order of precedence
of all C operators from high to low priority ?

I think this will save time rather than referring to the chart
for every expression.
Any ideas ?

Considering the below set of operator's order of precedence,
() [] . -> expr++ expr-- (Left to Right)

I made some trick as below for the order of precedence
of above operators :-
The (Flower) in the [box] has eyes(.) to see the (arrows)
(post)ed by plusplus(++) and minusinus(--)

Any other techniques apart from this or similar tricks for
remembering all the C operators order of precedence ?

Thx in advans,
Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
Is there a technique to easily remember the order of precedence
of all C operators from high to low priority ?

I think this will save time rather than referring to the chart
for every expression.

When in doubt, use parentheses.
 
K

karthikbalaguru

When in doubt, use parentheses.

For an expression as below
x < 5 && 2 * y < z

The parentheses can be put as below -
( x < 5 ) && ( 2 * ( y < z ) )
( x < 5 ) && ( ( 2 * y ) < z )

So, how does the method of using parentheses
help here ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

When in doubt, use parentheses.

Is there any link that explains about using
the parentheses for the correct evaluation
of an expression as per the order of
precedence of the operators in C ?

Thx in advans,
Karthik Balaguru
 
B

Bartc

karthikbalaguru said:
For an expression as below
x < 5 && 2 * y < z

The parentheses can be put as below -
( x < 5 ) && ( 2 * ( y < z ) )
( x < 5 ) && ( ( 2 * y ) < z )

So, how does the method of using parentheses
help here ?

If you want 2 * (y < z), use 2 * (y < z).
If you want (2 * y) < z, use (2 * y) < z.

You don't need to worry about what the default precedence is.
 
K

Kaz Kylheku

Hi,

Is there a technique to easily remember the order of precedence
of all C operators from high to low priority ?

What you can do is group them into categories. Memorize the precedence
among the categories, and within each category.

First there are the postfix operators, like function calls, array substripting
and postfix increment. They have the same precedence, and clump left to right.

Then the unary operators. They have the same precedence and build up right to
left.

Then comes the math category: add, subtract, multiply, divide, modulus. You
know these from grade school. Modulus % goes with multiplication and division.

Then you have to remember three categories, two of which are subdivided:

- shifting,
- relational,
- inequality
- equality
- logic:
- bitwise
- boolean

Mnemonics: shifting has an arithmetic basis, so it's close to the math
operations. Bitwise logic is also mathematical but because it resembles boolean
logic it is /dragged down/ and settles just above boolean logic. Relational
is in between shifting and logic because, well, relations are connections
that go /between/ things, and relational expressions are the connection
/between/ arithmetic and logic!

Relational less than and greater than tests like >= take precedence over
exact equality tests like != or ==. The way to remember that is:
a > b == b > c. That is, the result of less than or greater than is boolean,
and booleans may be compared using equality, but less than would not be applied
to booleans.

Within logic, you should know that AND has higher precedence than OR, which is
consistent with conventional logical notations. The bitwise exclusive or is
given an intermediate precedence between the two. Mnemonic: it's stronger than
OR (excludes the case when both are true) but weaker than AND (true in cases
where AND isn't). In the truth table, AND has only one truth, XOR has two, and
OR has three. One truth, two truths, three truths -> level 1, level 2, level 3
precedence.

The last three categories are:

- ternary operator
- assignments (plain and compounded with addition, shifting, etc)
- comma operator

You have to come up with something for yourself here.

You should remember that assignments have right to left associativity, and so
does the ternary operator. For assignments this is easy, since the flow of the
data is from right to left.
I think this will save time rather than referring to the chart
for every expression.

So, in summary, subdivide into levels, and conquer the precedence among the
levels and within the levels.

Don't be too clever with precedence. When writing code, clarify it with
parentheses.

Your goal for memorizing precedence should be that you can correctly /read/
even unclear expressions without having to look up precedence; your goal should
not be /writing/ expressions with the minimal possible use of parentheses, so
that others are forced to look up or memorize.

Cheers ...
 
K

Kojak

Le Wed, 18 Feb 2009 17:22:29 -0800 (PST),
karthikbalaguru a écrit :
For an expression as below
x < 5 && 2 * y < z

The parentheses can be put as below -
( x < 5 ) && ( 2 * ( y < z ) )
( x < 5 ) && ( ( 2 * y ) < z )

So, how does the method of using parentheses
help here ?

Parentheses help during writing code to ensure that it reflects
what you have in mind. In the first case, indeed you need to
remember the order of precedence of operator, but it's better
to avoid this kind of code.
 
K

Keith Thompson

Kojak said:
Le Wed, 18 Feb 2009 17:22:29 -0800 (PST),
karthikbalaguru a écrit :

Parentheses help during writing code to ensure that it reflects
what you have in mind. In the first case, indeed you need to
remember the order of precedence of operator, but it's better
to avoid this kind of code.

On the other hand, some precedence relationships should be easy to
remember with a bit of experience. I have no trouble remembering
the following, listed from tight to loose binding:

Unary (- + sizeof ~ !)
Multiplicative (* / %)
Additive (+ -)
Comparison (== != < <= > >=)
Logical (&& ||)
Assignment (= += *= et al)
Comma (,)

Note that operators within a row may or may not have the same
precedence.

Thus I likely wouldn't use any parentheses for
x < 5 && 2 * y < z
Then again, I'd probably use names that reflect the variables'
purposes better than x, y, and z.
 
K

karthikbalaguru

When in doubt, use parentheses.
Do you mean to convey that parentheses can be used to
evaluate expressions with multiple operators of different
precedence.

But, unless we know the order of precedence of the operators,
how can we use the parentheses.

Do you mean to convey that we can use parentheses to simplify
the identification of precedence without remembering
the order of precedence of the operators in C ?

Thx in advans,
Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
Do you mean to convey that parentheses can be used to
evaluate expressions with multiple operators of different
precedence.

But, unless we know the order of precedence of the operators,
how can we use the parentheses.

Do you mean to convey that we can use parentheses to simplify
the identification of precedence without remembering
the order of precedence of the operators in C ?

Suppose "@" and "$" are operators (they aren't), and you're not sure
of their relative precedence. I meant simply that if you don't know
how
x @ y $ z
will be evaluated, you can force whatever evaluation you like by
writing either
(x @ y ) $ z
or
x @ (y $ z)
whichever one suits your purpose.

One of these will be equivalent to the original unparenthesized
expression, and therefore in that one case the parentheses are,
strictly speaking, unnecessary. But they can still help a person
reading the code to understand your intent.
 
K

Kojak

Le Wed, 18 Feb 2009 18:16:14 -0800,
Keith Thompson a écrit :
On the other hand, some precedence relationships should be easy to
remember with a bit of experience. I have no trouble remembering
the following, listed from tight to loose binding:

Unary (- + sizeof ~ !)
Multiplicative (* / %)
Additive (+ -)
Comparison (== != < <= > >=)
Logical (&& ||)
Assignment (= += *= et al)
Comma (,)

Note that operators within a row may or may not have the same
precedence.

In this case we use associativity rules.
Thus I likely wouldn't use any parentheses for
x < 5 && 2 * y < z

Indeed this example is quite simple, especially for those of us
who are aware of precedence and associativity rules. That said,
few parentheses don't hurt and can help.
Then again, I'd probably use names that reflect the variables'
purposes better than x, y, and z.

For 3D Cartesian coordinate system? :)
 
C

CBFalconer

karthikbalaguru said:
Is there a technique to easily remember the order of precedence
of all C operators from high to low priority ?

I think this will save time rather than referring to the chart
for every expression. Any ideas ?

Just remember that multiplication (and division) takes precedence
over addition (and subtraction). For anything more complicated use
parentheses. You won't wonder, and your readers won't wonder.
 
K

Kaz Kylheku

Just remember that multiplication (and division) takes precedence
over addition (and subtraction). For anything more complicated use
parentheses. You won't wonder, and your readers won't wonder.

How do you know that karthik doesn't want to memorize precedence
for the purpose of being a more efficient reader of the language?

Right, missing the obvious again.
 
S

Stephen Sprunk

karthikbalaguru said:
For an expression as below
x < 5 && 2 * y < z

The parentheses can be put as below -
( x < 5 ) && ( 2 * ( y < z ) )
( x < 5 ) && ( ( 2 * y ) < z )

So, how does the method of using parentheses
help here ?

The primary audience of your code should be other programmers (including
yourself, in the future) who have to maintain it. Therefore, you should
make it as easy as possible for them to understand it -- not just the
minimum you know the compiler will understand.

When it comes to operator precedence, you can generally assume that
folks know multiplication and division occur before addition and
subtraction, just like they do in pencil-and-paper mathematics. For
everything else (unless it's a commonly used idiom like *p++ = *q++),
use parentheses to indicate what you _intend_ to happen, rather than
forcing many other programmers to look up the precedence of various
operators to understand what you intended the code to mean (assuming you
got it right...). They may not be necessary for the compiler in some
cases, but they're always helpful for humans, and that's what counts.

S
 
J

James Dow Allen

On the other hand, some precedence relationships should be easy to
remember with a bit of experience.  I have no trouble remembering
the following, listed from tight to loose binding:

    Unary (- + sizeof ~ !)
    Multiplicative (* / %)
    Additive (+ -)
    Comparison (== != < <= > >=)
    Logical (&& ||)
    Assignment (= += *= et al)
    Comma (,)

It's also easy to remember that ' >> ' has precedence adjacent
to ' > ' and '& ' has precedence adjacent to ' && '.
(Unfortunately >> is tighter than > while && looser than &,
but combining >> and > is rare anyway.) That ' == ' is
tighter than ' & ' is *wrong* but it's *famously* wrong,
which makes it easy to remember! :)

I disagree with "when in doubt use parentheses". Unnecessary
parentheses slow reading, and a good C programmer shouldn't
be reluctant to memorize precedence rules.

James Dow Allen
 
G

Guest

Le Wed, 18 Feb 2009 18:16:14 -0800,
Keith Thompson a écrit :


For 3D Cartesian coordinate system? :)

yes, but if you hard code the number of spatial dimensions
your code will not be portable to a space with more than three
dimensions
 
J

James Kuyper

karthikbalaguru said:
Hi,

Is there a technique to easily remember the order of precedence
of all C operators from high to low priority ?

I've never found any method for memorizing such stuff that works better
than the following: keep referring to the references whenever you are
confused, until it no long confuses you.

That probably doesn't count as "easy" - but it works pretty well for me.
 
J

James Dow Allen

but combining >> and > is rare anyway.

I can't honestly say this is not what I meant
to type, but it isn't what I *meant to think*!

"Combining" >> and > isn't so rare: I posted
a 1-line function with this combination just
7 weeks ago in a nearby ng:
/* is i just 1 or 2 consec bits? */
int tst3(unsigned int i)
{
return (i & -i) > (i >> 2);
}

What I *did* mean to think, I think, is that uses
of the combination of > and >> will almost always
follow the default precedence (parentheses
are unnecessary, though I included the
parentheses above to avoid complaint).

With the well-known exception of
if (ODDSENSE == number & 1) /* Oops. :-( */
the C precedence rules are what one needs and
expects. Even the precedence used in
if (v->party == Democrat
|| v->party == Republican && v->IQ > 110)
voter_might_have_a_clue(v);
follows standard symbolic logic practice.


PS: Sorry for not writing what I meant to write, or
even thinking what I meant to think. Nervous
tissue does deteriorate with age. (Sildenafil,
fortunately, restores youthful vigor to parts of
the autonomic nervous system but, AFAIK, is of
no value in cerebral function.)

James Dow Allen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top