The Wikipedia article on C and C++ operators

S

spibou

This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C++

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?title=Operators_in_C_and_C++&oldid=59984787

Cheers
Spiros Bousbouras
 
R

Richard Heathfield

[Followups set to comp.lang.c]

(e-mail address removed) said:

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.
 
A

Andrey Tarasevich

...
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.
 
V

Victor Bazarov

This concerns the Wikipedia article on C and C++ operators:
http://en.wikipedia.org/wiki/Operators_in_C_and_C++

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.

That would be wrong.

V
 
R

Robert Gamble

Victor said:
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
 
R

Robert Gamble

Andrey said:
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
 
A

Andrey Tarasevich

Andrey said:
...
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.
 
V

Victor Bazarov

Robert said:
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
 
A

Andrey Tarasevich

Robert said:
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.
 
S

spibou

Andrey said:
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++
has postfix with higher priority than prefix while the table at
http://en.wikipedia.org/w/index.php?title=Operators_in_C_and_C++&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
 
R

Robert Gamble

Victor said:
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
 
V

Victor Bazarov

Robert said:
Victor said:

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
 
R

Richard Heathfield

Victor Bazarov said:
Robert said:
Victor said:

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>
 
V

Victor Bazarov

Richard said:
Victor Bazarov said:
Robert said:
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?
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
 
R

Robert Gamble

Victor said:
Robert said:
Victor said:

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
 
J

Jerry Coffin

Victor Bazarov wrote:

[ ... ]
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.
 
V

Victor Bazarov

Robert said:
[..]
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...
 
S

spibou

Richard said:
Victor Bazarov said:
Robert said:
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.
 
A

Andrey Tarasevich

I was referring to priority of prefix vs. priority of postfix. The
table at
http://en.wikipedia.org/wiki/Operators_in_C_and_C++
has postfix with higher priority than prefix while the table at
http://en.wikipedia.org/w/index.php?title=Operators_in_C_and_C++&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.
 
V

Victor Bazarov

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
 

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,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top