Old-fashioned assignment operators (was Re: braces
Richard Michael Todd
rmtodd at uokmax.UUCP
Sat Dec 17 08:53:37 AEST 1988
In article <2151 at uokmax.UUCP> randy at uokmax.UUCP (Longshot) writes:
>As an interesting aside, the cc here on our Multimax will generate warnings for
>lines like:
>
> x=-y; /* x = -y or x =- y? */
>
>In other words, and math operator that can be combined with = can produce an
>error. This will happen with +,-, *, and any others that fit the bill. Now, I
>have yet to hard-test it to see which case it generates. Has anyone had
>similiar messages, and checked further?
Script started on Fri Dec 16 16:06:15 1988
% cat foo.c
main() {
int x,y;
x = 3;
y = 4;
x=-y;
printf("%d\n",x);
}
% cc foo.c
"foo.c", line 5: warning: old-fashioned assignment operator
% a.out
-1
% ^D
script done on Fri Dec 16 16:06:40 1988
It seems to be interpreting "=-" as "-=". The message warns of an
"old-fashioned" assignment operator. There's an interesting bit of history
here. Back around 6th Edition Unix (I think) the augmented assignment
operators in C were written as =+, =-, etc, not "+=" like we do today.
Unfortunately that caused an ambiguity problem: does x=-y mean x = -y or
x =- y? That, I believe, is why the operator syntax was changed circa
7th Edition. The compilers were changed to accept the old syntax "=-"
but issue a warning.
The question I have is, what exactly *is* the correct interpretation of
x=-y in modern C? As mentioned before, the Encore cc interprets it as
augmented assignment. The compiler I use on my PC interprets it as assigning
-y to x. Which interpretation is right? What does the ANSI C standard say
about this?
Of course, the existence of compilers that interpret this line differently
means that if you want your code to be portable, you *must* include proper
spaces; if you want to assign -y to x, you must say "x = -y" if you want
your code to work everywhere.
Finally, I question why this business of checking for old-fashioned
assignment operators is still in Unix C compilers. After all, V7 came
out around 10 years ago! Does anyone really have around any code that hasn't
been converted to the new syntax by *now*?
--
Richard Todd Fido:1:147/1 USSnail:820 Annie Court,Norman OK 73069
Try one of these: rmtodd at chinet.uucp, rmtodd at killer.dallas.tx.us,
rmtodd at uokmax.ecn.uoknor.edu or ...!sun!texsun!uokmax!rmtodd.
"MSDOS is a Neanderthal operating system" - Henry Spencer
More information about the Comp.unix.wizards
mailing list