Path: utzoo!attcan!uunet!ncrlnk!ncrcae!hubcap!gatech!ncar!mailrus!cornell!uw-beaver!microsoft!w-colinp From: w-colinp@microsoft.UUCP (Colin Plumb) Newsgroups: comp.lang.c Subject: Re: Comma Operator Message-ID: <267@microsoft.UUCP> Date: 14 Jan 89 09:28:31 GMT References: <922@quintus.UUCP> Reply-To: w-colinp@microsoft.uucp (Colin Plumb) Organization: very little Lines: 33 nair@quintus () wrote: > What should this print? > > int x, y; > printf("%d %d\n", (x = 1, y = 2), x, y); (I assume you really want a third %d in there.) It should print 2 . If a machine evaluates arguments left-to-right, it would print 2 1 2, but on many machines, arguments are pushed right-to-left, and evaluated in the same order. Thus, x and y get assigned after their old values have been pushed. C has never made any guarantees about the order in which arguments to functions are evaluated; while the comma operator does, the comma between function arguments is a different beast entirely. > Shouldn't it be equivalent to: > > int a, x, y; > a = (x = 1, y = 2); > printf("%d %d %d\n", a, x, y); Ah, this is different... there's a sequence point at the ; after the assignment to a, so it is guaranteed that x and y have their new values before the call is made. > Is there any justification in the first one printing > 2 1 0 Yes. -- -Colin (uunet!microsof!w-colinp)