home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
- From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
- Subject: Re: Question to test general C knowledge
- Message-ID: <1992Dec18.233716.8681@mksol.dseg.ti.com>
- Organization: Texas Instruments Inc
- References: <1992Dec10.214319.6692@leland.Stanford.EDU> <fjeske.0knf@amiganet.chi.il.us>
- Date: Fri, 18 Dec 1992 23:37:16 GMT
- Lines: 65
-
- In <fjeske.0knf@amiganet.chi.il.us> fjeske@amiganet.chi.il.us (Felix Jeske) writes:
-
- >> int i = 2;
- >> i = ++i;
- >> What should the value of i be?
- >>
- >>>3.
- >>
- >>BZZZT.
-
- >Double BZZZT!
-
- Triple BZZZT! [I normally find that kind of thing quite rude, but
- I'll play when dealing with someone who has that kind of mindset.]
-
- >The following:
-
- > i=++i;
-
- >should be broken up like when taking operator precedence into account:
-
- > ++i;
- > i=i;
-
- Look in the ANSI Standard. Show me where the sequence point is that
- lets you do the increment operator to 'i' first. Note that '=' is NOT
- a sequence point.
-
- >i is incremented (changing it's value to 3) and then is assigned to itself (a
- >no-op) so i=3 in the end.
-
- Where have you BEEN for the periodic discussions of this one, son? A
- bit late in the year for incoming Freshmen, isn't it?
-
- >>
- >> x = ++i;
- >>
- >>means that x is assigned the incremented value of i and
- >>i is incremented, it in no way requires an ordering of
- >>these two operations.
-
- >Yes it does, the precedence of the = and prefix ++ operators defines the order
- >of these operations quite clearly. Since a prefixed ++ has a higher precedence
- >(cf. K&R) than =, it (the ++) is performed first and the statement should be
- >broken up as follows:
-
- > ++i;
- > x=i;
-
- >No ambiguity whatsoever.
-
- Wrong all the way. See the ANSI C Standard. Side effects are not
- evaluated based on precedence. Increment and decrement operators have
- side effects. Side effects are only guaranteed to have occurred after
- a sequence point. The assignment operation is not a sequence point.
- Therefore, you have multiple references to 'i' in an expression
- involving side effects with no sequence point between the references.
-
- Can you say 'undefined behaviour'?
-
- --
- "Insisting on perfect safety is for people who don't have the balls to live
- in the real world." -- Mary Shafer, NASA Ames Dryden
- ------------------------------------------------------------------------------
- Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
-