C question

Paul Hoefling paulh at tektronix.UUCP
Sat Apr 6 04:24:45 AEST 1985


I was trying to send this to Dennis Ritchie, but I can't seem to find a
path to him.  So, I'll open it up for net discussion (and flames :-) ).

We've got a raging controversy going on here at Tektronix, and I thought
that I would appeal to you for an 'official' opinion.

---------------------------
The controversy:

An inexperienced C programmer wrote a program containing the following:

	x = x++;

Assuming that x originally had the value 5, what should the value be after
execution of the above expression ?

---------------------------
The arguments:

In favor of 5:
	According to the operator precedence table in K&R on page 49, the ++
operator has higher precedence than the = operator.  Hence the sequence of
operations should be:
	evaluate x (value == 5)
	increment x (x now == 6)
	assign the computed value to x (x now == 5 again)

In favor of 6:
	In K&R on page 42, the text says: "But the expression ++n increments n
*before* using its value, while n++ increments n *after* its value has been
used."  Hence, the sequence of operations should be:
	evaluate x (value == 5)
	assign the computed value to x (x still == 5)
	increment x (x now == 6)

---------------------------
Therefore, if the compiler writer follows the precedence table, 5 wins, and if
s/he follows the text, either could win.  Is there an 'official' opinion, and
if so, what is it ?

We are most interested in your response.

I would like to point out that the various compilers around here have been
tried, and we have 1 vote for 6 (our 4.2BSD compiler), and 3 votes for 5
(various microprocessor compilers).

If you send mail to me at ...!tektronix!paulh, I will collect the responses
and summarize for the net.

If Dennis Ritchie is out there somewhere, I would *really* appreciate his
opinion on the subject.
-- 

Paul Hoefling   (...!tektronix!paulh)
Information Pack Rat



More information about the Comp.lang.c mailing list