interesting program

Mike DeCorte mrd at sun.soe.clarkson.edu
Mon Feb 1 09:33:26 AEST 1988


Posting-Front-End: GNU Emacs 18.47.5 of Mon Jan 25 1988 on sun.soe.clarkson.edu (berkeley-unix)


In article <19 at berick.UUCP> jeffl at berick.UUCP (Jeff Lawhorn) writes:

   After watching all the flame wars here I decided that it was time to
   start one of my own  ;)

   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);
   }

There is no wrong answer to this! (assuming you answer either 4 or 5)
They are both right!

Reread page 50 of K&R.  If you have not read K&R DON'T tell people
what is and is not correct.

Here are the two possible interpetations.

1)
rk = 4  	(rk for rvlaue)
put rk into lk  (lk for lvalue)
increment lk    (value of lk = 5 now)

final answer = 5

2)
rk = 4  	(rk for rvlaue)
increment lk    (value of lk = 5 now)
put rk(4) into lk  (lk for lvalue)

final answer = 4

-- 
Michael DeCorte
mrd at clutx.clarkson.edu
mrd at clutx.bitnet



More information about the Comp.lang.c mailing list