By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

The Wikipedia article on C and C++ operators

P: n/a
This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Until very recently the first table in the page was a very
useful one on the precedence of C operators. This has
recently been replaced by one on C++ operators. I'm
not at all happy with that but I guess I can always link
to the history page. But I was wondering if anyone could
have a look and verify that the C part is correct. I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?

By the way here's the page I have been using:
http://en.wikipedia.org/w/index.php?...oldid=59984787

Cheers
Spiros Bousbouras

Jul 28 '06 #1
Share this Question
Share on Google+
52 Replies


P: n/a
[Followups set to comp.lang.c]

sp****@gmail.com said:

<snip>
But I was wondering if anyone could
have a look and verify that the C part [of a Wiki article] is correct.
No point. Even if it happens to be correct today, it could be wrong again by
this evening.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 28 '06 #2

P: n/a
sp****@gmail.com wrote:
...
I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
...
What do you mean by this? What exactly has "the same priority"?

Prefix increment and prefix decrement, for example, do have the same priority in
both C and C++, but this is not really something important because there is no
context where it would matter (since both are _prefix_). The same applies to
postfix operators.

When it comes to unary operators, the issue of "priority" only exists when we
compare a prefix operator to a postfix one. And in both C and C++ postfix unary
operators normally have higher priority than prefix ones.

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #3

P: n/a
sp****@gmail.com wrote:
This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Until very recently the first table in the page was a very
useful one on the precedence of C operators. This has
recently been replaced by one on C++ operators. I'm
not at all happy with that but I guess I can always link
to the history page. But I was wondering if anyone could
have a look and verify that the C part is correct. I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
In C++ postfix has higher precedence than prefix. The table seems fine
there.
I am certain that in C it is the same.

There is a circular dependency between ternary and assignment, the table
simply
cannot reflect that.
By the way here's the page I have been using:
http://en.wikipedia.org/w/index.php?...oldid=59984787
That would be wrong.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #4

P: n/a
Victor Bazarov wrote:
sp****@gmail.com wrote:
This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Until very recently the first table in the page was a very
useful one on the precedence of C operators. This has
recently been replaced by one on C++ operators. I'm
not at all happy with that but I guess I can always link
to the history page. But I was wondering if anyone could
have a look and verify that the C part is correct. I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?

In C++ postfix has higher precedence than prefix. The table seems fine
there.
I am certain that in C it is the same.

There is a circular dependency between ternary and assignment,
That statement makes absolutely no sense to me, can you expand on that,
perhaps provide and example to demonstrate what you are talking about?

Robert Gamble

Jul 28 '06 #5

P: n/a
Andrey Tarasevich wrote:
sp****@gmail.com wrote:
...
I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
...

What do you mean by this? What exactly has "the same priority"?

Prefix increment and prefix decrement, for example, do have the same priority in
both C and C++
Huh? You asked what the OP means by "same priority" and then you go on
to use the same term without an explanation of what *you* mean by it.
What *do* you mean, precedence? If so I would suggest you simply use
the word precedence since that is the term the Standard uses and the
word priority is obviously confusing (if not incorrect).

Robert Gamble

Jul 28 '06 #6

P: n/a
Andrey Tarasevich wrote:
...
When it comes to unary operators, the issue of "priority" only exists when we
compare a prefix operator to a postfix one. And in both C and C++ postfix unary
operators normally have higher priority than prefix ones.
...
A correction: in the last sentence the word "unary" is not needed. It was
supposed to be

[...] And in both C and C++ postfix operators normally have higher priority than
prefix ones.

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #7

P: n/a
Robert Gamble wrote:
Victor Bazarov wrote:
>sp****@gmail.com wrote:
>>This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Until very recently the first table in the page was a very
useful one on the precedence of C operators. This has
recently been replaced by one on C++ operators. I'm
not at all happy with that but I guess I can always link
to the history page. But I was wondering if anyone could
have a look and verify that the C part is correct. I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?

In C++ postfix has higher precedence than prefix. The table seems
fine there.
I am certain that in C it is the same.

There is a circular dependency between ternary and assignment,

That statement makes absolutely no sense to me, can you expand on
that, perhaps provide and example to demonstrate what you are talking
about?
You seem to be familiar with the Standard. Why don't you figure it out
by yourself by looking at the "Expressions" part of the Annex A, eh?

OK, OK, no need to escalate. You seem to be on edge as it is...

The ternary expression falls under "conditional-expression" part of the
grammar. The second part of it is "expression" and the third part of it
is "assignment-expression". So, we can write

c = a b ? a : b; /* assign larger of {a,b} to c */

which makes ternary higher in precedence than assingment, at the same
time if we write

c = a b ? a : b = a;

, the 'c' isn't only assigned the 'a' value, 'b' is also changed if it
is larger.

Try to position this correctly in the precedence table.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #8

P: n/a
Robert Gamble wrote:
Andrey Tarasevich wrote:
>sp****@gmail.com wrote:
...
I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
...

What do you mean by this? What exactly has "the same priority"?

Prefix increment and prefix decrement, for example, do have the same priority in
both C and C++

Huh? You asked what the OP means by "same priority" and then you go on
to use the same term without an explanation of what *you* mean by it.
What *do* you mean, precedence? If so I would suggest you simply use
the word precedence since that is the term the Standard uses and the
word priority is obviously confusing (if not incorrect).
I wasn't taking about the term "precedence" itself. The OP's statement is
ambiguous in a different way. It mentions two mutually exclusive properties at
the same time: postfix/prefix and increment/decrement. It is not clear what
priorities he is describing as being "the same". Priority of increment vs.
priority of decrement? Or priority of prefix vs. priority of postfix? Saying
that priorities of all four possible combinations are the same is technically
incorrect. They are not the same.

Then I proceeded to consider separately two general cases that I think are worth
considering in this case. That's all.

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #9

P: n/a
Andrey Tarasevich wrote:
Robert Gamble wrote:
Andrey Tarasevich wrote:
sp****@gmail.com wrote:
...
I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
...

What do you mean by this? What exactly has "the same priority"?

Prefix increment and prefix decrement, for example, do have the same priority in
both C and C++
Huh? You asked what the OP means by "same priority" and then you go on
to use the same term without an explanation of what *you* mean by it.
What *do* you mean, precedence? If so I would suggest you simply use
the word precedence since that is the term the Standard uses and the
word priority is obviously confusing (if not incorrect).

I wasn't taking about the term "precedence" itself. The OP's statement is
ambiguous in a different way. It mentions two mutually exclusive properties at
the same time: postfix/prefix and increment/decrement. It is not clear what
priorities he is describing as being "the same". Priority of increment vs.
priority of decrement? Or priority of prefix vs. priority of postfix?
I was referring to priority of prefix vs. priority of postfix. The
table at
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
has postfix with higher priority than prefix while the table at
http://en.wikipedia.org/w/index.php?...oldid=59984787
has them with the same priority. Although I can't think of a
practical situation where the discrepancy would make a difference.

Anyway , can anyone point me to a reliable table on the net
about the priority of C operators ?

Spiros Bousbouras

Jul 28 '06 #10

P: n/a
Victor Bazarov wrote:
Robert Gamble wrote:
Victor Bazarov wrote:
sp****@gmail.com wrote:
This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Until very recently the first table in the page was a very
useful one on the precedence of C operators. This has
recently been replaced by one on C++ operators. I'm
not at all happy with that but I guess I can always link
to the history page. But I was wondering if anyone could
have a look and verify that the C part is correct. I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?

In C++ postfix has higher precedence than prefix. The table seems
fine there.
I am certain that in C it is the same.

There is a circular dependency between ternary and assignment,
That statement makes absolutely no sense to me, can you expand on
that, perhaps provide and example to demonstrate what you are talking
about?

The ternary expression falls under "conditional-expression" part of the
grammar. The second part of it is "expression" and the third part of it
is "assignment-expression". So, we can write

c = a b ? a : b; /* assign larger of {a,b} to c */

which makes ternary higher in precedence than assingment, at the same
time if we write
The conditional operator has higher precedence than assignment,
correct.
c = a b ? a : b = a;

, the 'c' isn't only assigned the 'a' value, 'b' is also changed if it
is larger.
You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. You seem to think that your example is
equivalent to:

((c = a) b) ? a : (b = a);

which is incorrect. It is actually equivalent to:

c = ((a b ? a : b) = a);

which isn't even a valid c statement.
Try to position this correctly in the precedence table.
....
Logical OR operator
Conditional operator
Assignment operators
Comma operator

Robert Gamble

Jul 28 '06 #11

P: n/a
Robert Gamble wrote:
Victor Bazarov wrote:
>[..]

You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. [..]
You cross-posted this to both C and C++ newsgroups. My explanation
concerns the behaviour of the ternary and assignment operators as it
is in C++. I have no misunderstanding of C precedence since I have
never attempted to understand it, nor am I interested. Good day!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #12

P: n/a
Victor Bazarov said:
Robert Gamble wrote:
>Victor Bazarov wrote:
>>[..]

You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. [..]

You cross-posted this to both C and C++ newsgroups.
Well, the OP did, without setting a followup.
My explanation
concerns the behaviour of the ternary and assignment operators as it
is in C++.
The question was specifically and explicitly about C, and should never have
been posted to comp.lang.c++.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 28 '06 #13

P: n/a
Richard Heathfield wrote:
Victor Bazarov said:
>Robert Gamble wrote:
>>Victor Bazarov wrote:
[..]

You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. [..]

You cross-posted this to both C and C++ newsgroups.

Well, the OP did, without setting a followup.
Why would follow-up be needed?
>My explanation
concerns the behaviour of the ternary and assignment operators as it
is in C++.

The question was specifically and explicitly about C, and should
never have been posted to comp.lang.c++.
REALLY? What makes you think that? Could it be this part of the
orginal post that gives it away:
... I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
^^^^^^^^^^^^^^^^^^^^^^

The Wikipedia article claims to be about the operators in C *and* C++.
The table is indeed titled "operators in the C programming language".
My point was to indicate that it's not so easy in C++ WRT ternary and
assignment. I guess I'd failed to communicate that I was talking
about C++, for which such table cannot be easily created, namely due
to the magic of ternary and assignment.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #14

P: n/a
Victor Bazarov wrote:
Robert Gamble wrote:
Victor Bazarov wrote:
[..]
You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. [..]

You cross-posted this to both C and C++ newsgroups.
Your post was cross-posted to C (as was the original post for some
reason), maybe you didn't realize this, I didn't change the
cross-postings when following up to your original response.
My explanation
concerns the behaviour of the ternary and assignment operators as it
is in C++. I have no misunderstanding of C precedence since I have
never attempted to understand it, nor am I interested. Good day!
Since the OP was asking about C I assumed your explanation of the
conditional operator was from a C standpoint, I apologize for the
confusion. Your explanation is correct for C++. The third part of the
conditional expression in C++ is "assignment expression", in C it is
"conditional expression" which is why this isn't valid in C.

Robert Gamble

Jul 28 '06 #15

P: n/a
In article <11**********************@m79g2000cwm.googlegroups .com>,
rg*******@gmail.com says...
Victor Bazarov wrote:
[ ... ]
c = a b ? a : b = a;

, the 'c' isn't only assigned the 'a' value, 'b' is also changed if it
is larger.

You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. You seem to think that your example is
equivalent to:

((c = a) b) ? a : (b = a);

which is incorrect. It is actually equivalent to:

c = ((a b ? a : b) = a);

which isn't even a valid c statement.
I think the discussion here is taking place at cross-purposes,
probably due to the cross-posting between c.l.c and c.l.c++.

In C, a conditional expression is defined as:

logical-OR-expression ? expression : conditional-expression

But in C++, a conditional expression is defined as:

logical-or-expression ? expression : assignment-expression

As such, in C you're correct -- the result isn't valid. In C++,
however, the result is perfectly valid, and equivalent to:

c = ((a b) ? a : (b = a);

i.e. it checks whether a is greater than b. If it is, it assigns a to
c. Otherwise, it assigns a to b and the result of that to c. I'll
have to admit, it's pretty rare that you'd want to do that though.
Under normal circumstances you could get the same result much more
readably with:

if (a <= b)
b = a;
c = a;

If you were doing something with operator overloading that made this
significantly different, chances are you should re-think it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 28 '06 #16

P: n/a
Robert Gamble wrote:
[..]
Since the OP was asking about C
The OP was asking about *both*. The Wikipedia article is about *both*.
[..] The third part of
the conditional expression in C++ is "assignment expression", in C it
is "conditional expression" which is why this isn't valid in C.
The second part of the ternary variant of 'conditional-expression' in
C is 'expression'. So in C I should be albe to write

b < a ? b , a : a , b;

Which can be quite confusing even in C, don't you think? How does it
fit into lower precedence of the comma operator? Anyway, I lost
interest in this discussion...
Jul 28 '06 #17

P: n/a
sp****@gmail.com wrote:
>
I was referring to priority of prefix vs. priority of postfix. The
table at
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
has postfix with higher priority than prefix while the table at
http://en.wikipedia.org/w/index.php?...oldid=59984787
has them with the same priority. Although I can't think of a
practical situation where the discrepancy would make a difference.
That's where some differences between C and C++ come into play.

In C, from a formal point of view (as dictated by the language grammar),
precedence of postfix increment/decrement _is_ higher than that of prefix
increment/decrement. But the need to compare the two might only arise in
expressions like

++v--; /* it is '++(v--)', not (++v)-- */

which is well-formed, but produces undefined behavior. This means that there are
no practically useful contexts where it might be necessary to compare these
precedences and it is OK to describe them as "the same".

In C++ the above is true only for built-in increment/decrement operators. Once
we start considering overloaded increment/decrement operators (which, of course,
have the same precedence as built-in ones), the above expression no longer
produces undefined behavior and knowing that it is interpreted as '++(v--)'
becomes important.
Anyway , can anyone point me to a reliable table on the net
about the priority of C operators ?
I don't immediately see any problems with the original C table linked to your
message (taking into account what I sad above). The new (C++) table also looks
fine to me. And it will also work fine for C (at least in the
increment/decrement department).

There's a little issue with the way ternary '?:' operator is parsed in C and C++
(see Victor's messages), but that's something one just has to remember.

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #18

P: n/a
Richard Heathfield wrote:
Victor Bazarov said:
Robert Gamble wrote:
Victor Bazarov wrote:
[..]

You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. [..]
You cross-posted this to both C and C++ newsgroups.

Well, the OP did, without setting a followup.
I have no idea what a followup is or how to set one !

And in case people haven't noticed, I'm still looking for
a reliable table of C operators precedence on the net. Surely
a lot of people would have a use for such a thing.

Jul 28 '06 #19

P: n/a
sp****@gmail.com wrote:
And in case people haven't noticed, I'm still looking for
a reliable table of C operators precedence on the net. Surely
a lot of people would have a use for such a thing.
So why did you mix C++ into it? Anyway, the one on the Wikipedia
page you asked about seems to be fine.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 28 '06 #20

P: n/a
I'm still looking for a reliable table of C operators precedence on the
net.
Here's the one I use for C++:

http://www.difranco.net/cop2334/cpp_op_prec.htm

(I actually saved it to my hard disk, and have a little icon for it on my
Windows Taskbar.)

--

Frederick Gotham
Jul 28 '06 #21

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote:
The ternary expression falls under "conditional-expression" part of the
grammar. The second part of it is "expression" and the third part of it
is "assignment-expression". So, we can write

c = a b ? a : b; /* assign larger of {a,b} to c */

which makes ternary higher in precedence than assingment, at the same
time if we write

c = a b ? a : b = a;

, the 'c' isn't only assigned the 'a' value, 'b' is also changed if it
is larger.

Try to position this correctly in the precedence table.
I tried compiling that in a C program, and gcc gives me
"error: invalid lvalue in assignment". But oddly, when
I change the file-name suffix from "c" to "cpp" and compile
with gpp instead of gcc, it compiles without error.

So apparently there's a major difference between how C vs. C++
handle that line of code.

I wonder if the semantics are guaranteed even if the syntax
makes it through the compiler? Perhaps it's another case like:

a[i] = i++;
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
home dot pac bell dot net slant earnur slant
Jul 28 '06 #22

P: n/a


Andrey Tarasevich wrote On 07/28/06 14:49,:
sp****@gmail.com wrote:
>>I was referring to priority of prefix vs. priority of postfix. The
table at
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
has postfix with higher priority than prefix while the table at
http://en.wikipedia.org/w/index.php?...oldid=59984787
has them with the same priority. Although I can't think of a
practical situation where the discrepancy would make a difference.


That's where some differences between C and C++ come into play.

In C, from a formal point of view (as dictated by the language grammar),
precedence of postfix increment/decrement _is_ higher than that of prefix
increment/decrement. But the need to compare the two might only arise in
expressions like

++v--; /* it is '++(v--)', not (++v)-- */

which is well-formed, but produces undefined behavior.
It produces a compiler diagnostic. It is syntactically
well-formed, but violates a semantic constraint: the operand
of ++ is not an lvalue. ++42 is syntactically well-formed
and violates the same constrant.

(Of course, the compiler is permitted to accept the
program and generate code in addition to emitting the
required diagnostic; the effect of executing any such
program would, as you say, be undefined.)

--
Er*********@sun.com

Jul 28 '06 #23

P: n/a
Eric Sosman wrote:
>...
In C, from a formal point of view (as dictated by the language grammar),
precedence of postfix increment/decrement _is_ higher than that of prefix
increment/decrement. But the need to compare the two might only arise in
expressions like

++v--; /* it is '++(v--)', not (++v)-- */

which is well-formed, but produces undefined behavior.

It produces a compiler diagnostic. It is syntactically
well-formed, but violates a semantic constraint: the operand
of ++ is not an lvalue. ++42 is syntactically well-formed
and violates the same constrant.
...
You are right. I missed that

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #24

P: n/a
Andrey Tarasevich posted:
++v--; /* it is '++(v--)', not (++v)-- */

which is well-formed, but produces undefined behavior.

<OFF TOPIC>

This is more of a nitpick than anything else, but if we're talking about C++,
then it's possible for such a statement to be pefectly OK, depending on the
definitions of operator++ and operator--(int).

</OFF TOPIC>

--

Frederick Gotham
Jul 28 '06 #25

P: n/a
Frederick Gotham wrote:
Andrey Tarasevich posted:
> ++v--; /* it is '++(v--)', not (++v)-- */

which is well-formed, but produces undefined behavior.


<OFF TOPIC>

This is more of a nitpick than anything else, but if we're talking about C++,
then it's possible for such a statement to be pefectly OK, depending on the
definitions of operator++ and operator--(int).

</OFF TOPIC>
But that's exactly what I said in the next paragraph of the message you were
replying to.

--
Best regards,
Andrey Tarasevich
Jul 28 '06 #26

P: n/a
sp****@gmail.com wrote:
Richard Heathfield wrote:
Victor Bazarov said:
Robert Gamble wrote:
>Victor Bazarov wrote:
>>[..]

>You seem to have a fundamental misunderstanding of the effective
>precedence of operators in C. [..]
>
You cross-posted this to both C and C++ newsgroups.
Well, the OP did, without setting a followup.

I have no idea what a followup is or how to set one !

That's fine, because you should not have set one.

Brian
Jul 28 '06 #27

P: n/a
Victor Bazarov said:
Richard Heathfield wrote:
>Victor Bazarov said:
>>Robert Gamble wrote:
Victor Bazarov wrote:
[..]

You seem to have a fundamental misunderstanding of the effective
precedence of operators in C. [..]

You cross-posted this to both C and C++ newsgroups.

Well, the OP did, without setting a followup.

Why would follow-up be needed?
Cross-posting is generally a bad idea (although it's a better idea than
multi-posting, of course). When it must be done, 'twere best 'twere done
with a follow-up to a particular group.
>>My explanation
concerns the behaviour of the ternary and assignment operators as it
is in C++.

The question was specifically and explicitly about C, and should
never have been posted to comp.lang.c++.

REALLY? What makes you think that? Could it be this part of the
orginal post that gives it away:
>... I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
^^^^^^^^^^^^^^^^^^^^^^
Oops. Yes, I did indeed miss that part of it. I'm not sure it was necessary
to yell, though.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 28 '06 #28

P: n/a
Victor Bazarov said:
sp****@gmail.com wrote:
>And in case people haven't noticed, I'm still looking for
a reliable table of C operators precedence on the net. Surely
a lot of people would have a use for such a thing.

So why did you mix C++ into it? Anyway, the one on the Wikipedia
page you asked about seems to be fine.
For now[1]. Give it time.
[1] Maybe. I didn't look. I didn't see the point.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 28 '06 #29

P: n/a
Richard Heathfield wrote:

Cross-posting is generally a bad idea (although it's a better idea
than multi-posting, of course). When it must be done, 'twere best
'twere done with a follow-up to a particular group.

NO NO NO!
It's very rude to cross-post and set follow-ups. What are people in the
other groups supposed to do, subscribe to some other group to continue
the thread? If the answers don't belong in all the groups, the original
post didn't either. The only legitimate use of follow-up-to is to
redirect an off-topic post to another group. It should never be a
deliberate method by the original poster.

Brian
Jul 28 '06 #30

P: n/a
Victor Bazarov wrote:
Robert Gamble wrote:
[..]
Since the OP was asking about C

The OP was asking about *both*. The Wikipedia article is about *both*.
Well, sort of. The OP was looking for a C precedence table and asked
if the one that appeared on a page about C and C++ and contained C++
operators would work for C. The OP didn't seem interested in how
things work in C++, just if the table provided was compatible with C,
given that context I think my interpreation of your example (which
didn't specify C or C++) was reasonable.
[..] The third part of
the conditional expression in C++ is "assignment expression", in C it
is "conditional expression" which is why this isn't valid in C.

The second part of the ternary variant of 'conditional-expression' in
C is 'expression'. So in C I should be albe to write

b < a ? b , a : a , b;
Yes, it would be the same thing as:

(b < a ? (b , a) : a) , b;
Which can be quite confusing even in C, don't you think?
Not nearly as much so as the C++ example you provided which I think is
quite unintuitive.
How does it
fit into lower precedence of the comma operator?
The conditional operator is special in that it is the only ternary
operator in the language. The ? and : symbols delimit the enclosing
expression making "a ? b : c" equivalent to "a ? (b) : c". Once one
realizes this it makes sense that an expression with lower precedence
operators are handled the way they are, there is not precendence
inconsistency.

Robert Gamble

Jul 28 '06 #31

P: n/a
Default User said:
Richard Heathfield wrote:

>Cross-posting is generally a bad idea (although it's a better idea
than multi-posting, of course). When it must be done, 'twere best
'twere done with a follow-up to a particular group.

NO NO NO!
Or yes.
It's very rude to cross-post and set follow-ups.
I beg your leave to differ. Cross-posting should be avoided if possible. If
it cannot be avoided, it should at least be minimised. Setting a follow-up
contributes to this goal.

What are people in the
other groups supposed to do, subscribe to some other group to continue
the thread?
Yes, if they care enough. No, if they don't.

<snip>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 28 '06 #32

P: n/a
Richard Heathfield wrote:
Default User said:
Richard Heathfield wrote:

Cross-posting is generally a bad idea (although it's a better idea
than multi-posting, of course). When it must be done, 'twere best
'twere done with a follow-up to a particular group.
NO NO NO!

Or yes.
It's very rude to cross-post and set follow-ups.

I beg your leave to differ. Cross-posting should be avoided if
possible. If it cannot be avoided, it should at least be minimised.
Setting a follow-up contributes to this goal.
There are no circumstances where it can't be avoided. Any use is
completely discretionary. You can't convince me that it is anything
other than extremely rude to say, "Here's a cool topic, but you can't
see the answers here! Come on over to xxx.yyy.zzz. I don't care that
you don't subscribe!"
>
What are people in the
other groups supposed to do, subscribe to some other group to
continue the thread?

Yes, if they care enough. No, if they don't.
I just cannot agree with this. If the reply doesn't belong, it should
not have appeared in the first place. If the post was topical, then so
are the replies.

To ask people to move to another group is rude and thoughtless in my
opinion. I will say so emphatically every time I see someone suggest it.

Brian
Jul 28 '06 #33

P: n/a
Default User said:

<snip>
To ask people to move to another group is rude and thoughtless in my
opinion. I will say so emphatically every time I see someone suggest it.
I'm not sure where this matter ought to be discussed, but it's clearly no
longer topical in clc or clc++.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 28 '06 #34

P: n/a
Richard Heathfield wrote:
Default User said:

<snip>
To ask people to move to another group is rude and thoughtless in my
opinion. I will say so emphatically every time I see someone
suggest it.

I'm not sure where this matter ought to be discussed, but it's
clearly no longer topical in clc or clc++.
You don't consider it to be metatopical? We are discussing how to post
in the relevant newsgroups. I have no desire to argue with you about
it, you know my feelings on the subject.


Brian
Jul 28 '06 #35

P: n/a
Victor Bazarov wrote:
>
In C++ postfix has higher precedence than prefix. The table seems fine
there. I am certain that in C it is the same.
Can you give an example of that? I think both versions of ++ should
be in the lower category, although I can't think of any examples where
the behaviour is defined, and there would be a difference.

Jul 29 '06 #36

P: n/a
Old Wolf wrote:
Victor Bazarov wrote:
>>
In C++ postfix has higher precedence than prefix. The table seems
fine there. I am certain that in C it is the same.

Can you give an example of that?
An example of what?

int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}

Or the idiomatic

*ptr++

(first increment, then dereference using the old value).
I think both versions of ++ should
be in the lower category, although I can't think of any examples where
the behaviour is defined, and there would be a difference.
In lower category? Meaning what? How would you deal with grouping of the
operations? Left-to-right first? Right-to-left first? How would you
deal with this:

int arr[10] = {1,2,3}, *pa = arr, b = pa++[0];

? The higher-precedence indexing applied to the result of the lower-
precedence increment? Notice that all higher precedence operators are
postfix (increment/decrement, function call, subscript, member access
for objects, member access for pointers) and all lower precedence
operators are prefix - increment/decrement, unary plus/minus, not and
bitwise zero complement, type cast, dereference and address-of.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 29 '06 #37

P: n/a
Default User posted:
You can't convince me that it is anything other than extremely rude to
say, "Here's a cool topic, but you can't see the answers here! Come on
over to xxx.yyy.zzz. I don't care that you don't subscribe!"

Maybe we should abolish all of the newsgroups, and everyone should just post
to alt.talk.

--

Frederick Gotham
Jul 29 '06 #38

P: n/a
Andrey Tarasevich posted:
><OFF TOPIC>

This is more of a nitpick than anything else, but if we're talking
about C++, then it's possible for such a statement to be pefectly OK,
depending on the definitions of operator++ and operator--(int).

</OFF TOPIC>

But that's exactly what I said in the next paragraph of the message you
were replying to.

I jumped the gun, sorry.

--

Frederick Gotham
Jul 29 '06 #39

P: n/a
Victor Bazarov wrote:
Old Wolf wrote:
Victor Bazarov wrote:
>
In C++ postfix has higher precedence than prefix. The table seems
fine there. I am certain that in C it is the same.
Can you give an example of that?

An example of what?
An example showing where putting postfix ++ in the lower category
would give a different way of parsing than if it's in the higher
category.
int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}
An example that compiles and has defined behaviour.
Or the idiomatic

*ptr++

(first increment, then dereference using the old value).
You're confusing precedence with order of evaluation
(either that or I miss your point). In that example,
postfix ++ could be in the lower category (ie. the same
category as unary *), because that category is
right-associative, so it is *(ptr++) .
I think both versions of ++ should
be in the lower category, although I can't think of any examples where
the behaviour is defined, and there would be a difference.

In lower category? Meaning what?
In the category of prefix ++, in the table which is the topic
of this thread.
How would you deal with grouping of the
operations? Left-to-right first? Right-to-left first?
Right to left, as stated in that table.
How would you deal with this:

int arr[10] = {1,2,3}, *pa = arr, b = pa++[0];
The only possible ways of parsing that are:
... ((b = pa)++)[0]
and
... b = ((pa++)[0])
and
... (b = (pa++))[0]

and the precedence of ++ is higher than that of
=, so the first is out; and the precedence of [] is higher
than that of =, so the last is out. The relative precedence
of [] and ++ is not involved in this one.
? The higher-precedence indexing applied to the result of the lower-
precedence increment?
The way it is written, there is not a precedence decision involved.

Jul 29 '06 #40

P: n/a

sp****@gmail.com wrote:
This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Until very recently the first table in the page was a very
useful one on the precedence of C operators. This has
recently been replaced by one on C++ operators. I'm
not at all happy with that but I guess I can always link
to the history page. But I was wondering if anyone could
have a look and verify that the C part is correct. I'm pretty
sure that in C postfix/prefix increment/decrement have
the same priority. Is it different in C++ ?
Hang on. Are you saying that the table of precedence for C++ operators,
now has precedence over the table of precedence for the table of C
operators, whereas previously the table now preceeding was in fact
previously not preceeding the table which previously preceeded it? If
so the correct procedure is to proceed to replace if possible these
tables of precedence in their previous precedence. Simple really ...

regards
Andy Little

Jul 29 '06 #41

P: n/a
Old Wolf wrote:
Victor Bazarov wrote:
> int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}

An example that compiles and has defined behaviour.
REALLY? On which compiler? I am asking so that I can avoid it like
a plague.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 29 '06 #42

P: n/a
Victor Bazarov wrote:
Old Wolf wrote:
Victor Bazarov wrote:
int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}
An example that compiles and has defined behaviour.

REALLY? On which compiler? I am asking so that I can avoid it like
a plague.
Any conforming compiler (?)

Jul 30 '06 #43

P: n/a
Old Wolf posted:
Victor Bazarov wrote:
>Old Wolf wrote:
Victor Bazarov wrote:
int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}

An example that compiles and has defined behaviour.

REALLY? On which compiler? I am asking so that I can avoid it like
a plague.

Any conforming compiler (?)

I wouldn't have much faith in a compiler which didn't give a warning or an
error for:

int main()
{
++5;
}

--

Frederick Gotham
Jul 30 '06 #44

P: n/a
"Victor Bazarov" <v.********@comAcast.netwrites:
Old Wolf wrote:
>Victor Bazarov wrote:
>> int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}

An example that compiles and has defined behaviour.

REALLY? On which compiler? I am asking so that I can avoid it like
a plague.
I think there was a misunderstanding here. Old Wolf was asking for
such an example; "++a++;" isn't one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 30 '06 #45

P: n/a
Frederick Gotham wrote:
Old Wolf posted:
Victor Bazarov wrote:
Old Wolf wrote:
Victor Bazarov wrote:
int main() {
int a = 42;
++a++; /* shouldn't compile: prefix applied to an r-value */
}

An example that compiles and has defined behaviour.

REALLY? On which compiler? I am asking so that I can avoid it like
a plague.
Any conforming compiler (?)

I wouldn't have much faith in a compiler which didn't give a warning or an
error for:

int main()
{
++5;
}
What does that have to do with the topic at hand? I'm asking
for an example that compiles and has defined behaviour, of
code where assigning postfix++ the same priority as prefix++,
conflicts with the specification of the standard. Victor's example
and your example aren't such examples.

Jul 30 '06 #46

P: n/a
Old Wolf wrote:
[..] I'm asking
for an example that compiles and has defined behaviour, of
code where assigning postfix++ the same priority as prefix++,
conflicts with the specification of the standard.
You weren't, before. If you want to assign the same precedence to
those operators, you have to define first how they are going to work,
i.e. what the order of their processing is going to be, and how they
are going to interact with other operators.
Victor's example
and your example aren't such examples.
I gave you the example you asked for. Post-xxcrement has higher
precedence, so it's applied first. All postfix operators are
applied first *because* they have higher precedence. As soon as
you give them the same precedence as prefix operators, the grammar
breaks down.

You say "move them all to the same precedence". How? Which order
first? Left-to-right or right-to-left? Or mixed? One from left
and one from right, or reversed?

Stop wasting our time.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '06 #47

P: n/a
"Victor Bazarov" <v.********@comAcast.netwrites:
Old Wolf wrote:
>[..] I'm asking
for an example that compiles and has defined behaviour, of
code where assigning postfix++ the same priority as prefix++,
conflicts with the specification of the standard.

You weren't, before. If you want to assign the same precedence to
those operators, you have to define first how they are going to work,
i.e. what the order of their processing is going to be, and how they
are going to interact with other operators.
>Victor's example
and your example aren't such examples.

I gave you the example you asked for. Post-xxcrement has higher
precedence, so it's applied first. All postfix operators are
applied first *because* they have higher precedence. As soon as
you give them the same precedence as prefix operators, the grammar
breaks down.
Your example included the line

++a++;

This is illegal in C, since neither prefix ++ nor postfix ++ yields an
lvalue. If you want to discuss it in the context of C++, please drop
comp.lang.c from the Newsgroups: header.

This is an excellent example of why cross-posts to comp.lang.c and
comp.lang.c++ are almost always a bad idea.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 30 '06 #48

P: n/a
Follow-ups set to c.l.c++ , as that is where Victor is reading

Victor Bazarov wrote:
Old Wolf wrote:
[..] I'm asking
for an example that compiles and has defined behaviour, of
code where assigning postfix++ the same priority as prefix++,
conflicts with the specification of the standard.

You weren't, before.
>From my original post on this thread:
Victor Bazarov wrote:
>In C++ postfix has higher precedence than prefix. The table seems fine
there. I am certain that in C it is the same.
Can you give an example of that? I think both versions of ++ should
be in the lower category
I thought it went without saying that the example should be
well-defined.
If you want to assign the same precedence to
those operators, you have to define first how they are going to work,
i.e. what the order of their processing is going to be, and how they
are going to interact with other operators.
Please see the precedence table referenced from the initial
message in this thread.
Victor's example
and your example aren't such examples.

I gave you the example you asked for.
Your example causes undefined behaviour.
Post-xxcrement has higher precedence, so it's applied first.
All postfix operators are applied first *because* they have
higher precedence. As soon as you give them the same
precedence as prefix operators, the grammar breaks down.
This is nonsense. Again, I ask you to give a program which is
well-defined according to the table as currently given, but would
be different if postfix ++ were moved to the same precedence
group as prefix ++.
You say "move them all to the same precedence". How? Which order
first? Left-to-right or right-to-left? Or mixed? One from left
and one from right, or reversed?

Stop wasting our time.
You continually ask the same question, which I have already
answered explicitly, and was covered by the table in the initial
message in this thread.

Again: postfix-++ should have the same precedence as prefix-++
in the table, and be right-to-left associative.

If you think my question is a waste of time, simply stop replying.

BTW, is English your first language? (I don't mean this as a flame,
I am just curious, as we seem to have a bit of a gap in understanding
here).

Jul 31 '06 #49

P: n/a
Andrey Tarasevich <an**************@hotmail.comwrote:
sp****@gmail.com wrote:

I was referring to priority of prefix vs. priority of postfix. The
table at
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
has postfix with higher priority than prefix while the table at
http://en.wikipedia.org/w/index.php?...oldid=59984787
has them with the same priority. Although I can't think of a
practical situation where the discrepancy would make a difference.

That's where some differences between C and C++ come into play.

In C, from a formal point of view (as dictated by the language grammar),
precedence of postfix increment/decrement _is_ higher than that of prefix
increment/decrement. But the need to compare the two might only arise in
expressions like

++v--; /* it is '++(v--)', not (++v)-- */
The main practical reason is, of course, that it's not just postfix ++
which has a higher priority than prefix ++, but _all_ postfix operators
have higher priority than all unary (prefix) operators. This also means
that, for example, -a[10] is -(a[10]), not (-a)[10] - which is the
reasonable way for the priority to be.

Richard
Jul 31 '06 #50

52 Replies

This discussion thread is closed

Replies have been disabled for this discussion.