No sequence points in assignment

Jeffrey Kegler jeffrey at algor2.algorists.com
Wed Sep 13 10:52:47 AEST 1989


In article <1021 at m3.mfci.UUCP> karzes at mfci.UUCP (Tom Karzes) writes:
>Consider the following statement:
>
>    x = a + (x = b);
>
>Could the assignment for (x = b) be performed after the outer
>assignment?

In ANSI C (3.3), this is undefined behavior, meaning anything (core
dump, generation of random number, etc.) can happen.  In all
likelihood, and in Classic C, you get the assignments performed in
whatever order amuses the compiler.

In any case, the practical implications are the same.  Anyone coming
across statements like this in code should rearrange them to introduce
a sequence point.  Breaking the above into two statements, or
rearrangement around a comma operator, will do it in the above.
-- 

Jeffrey Kegler, Independent UNIX Consultant, Algorists, Inc.
jeffrey at algor2.ALGORISTS.COM or uunet!algor2!jeffrey
1762 Wainwright DR, Reston VA 22090



More information about the Comp.std.c mailing list