Path: utzoo!utgpu!water!watmath!clyde!rutgers!rochester!crowl From: crowl@cs.rochester.edu (Lawrence Crowl) Newsgroups: comp.lang.c Subject: Re: interesting program Message-ID: <6378@sol.ARPA> Date: 30 Jan 88 04:21:28 GMT References: <19@berick.UUCP> Reply-To: crowl@cs.rochester.edu (Lawrence Crowl) Organization: U of Rochester, CS Dept, Rochester, NY Lines: 26 In article <19@berick.UUCP> jeffl@berick.UUCP (Jeff Lawhorn) writes: ]What does the following program print? (I know of at least one ]program that prints the wrong answer). ] ]main() { ] int k = 4; ] k = k++; ] printf("and the answer is %d\n", k); ] exit(0); ]} This program is ambiguous. The statement "k = k++;" depends on the order of evaluation, which is undefined. Valid C implementation may print either 4 or 5. So, unless the C compiler in question is really bad, it does not provide a "wrong" answer. Consider the following legal executions for the statement in question: r := k ; r := k ; k := k + 1 ; k := r ; k := r ; k := k + 1 ; Both meet the standard, but yield different results. -- Lawrence Crowl 716-275-9499 University of Rochester crowl@cs.rochester.edu Computer Science Department ...!{allegra,decvax,rutgers}!rochester!crowl Rochester, New York, 14627