home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18657 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.5 KB  |  76 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
  3. From: mccall@mksol.dseg.ti.com (fred j mccall 575-3539)
  4. Subject: Re: Question to test general C knowledge
  5. Message-ID: <1992Dec18.233716.8681@mksol.dseg.ti.com>
  6. Organization: Texas Instruments Inc
  7. References: <1992Dec10.214319.6692@leland.Stanford.EDU> <fjeske.0knf@amiganet.chi.il.us>
  8. Date: Fri, 18 Dec 1992 23:37:16 GMT
  9. Lines: 65
  10.  
  11. In <fjeske.0knf@amiganet.chi.il.us> fjeske@amiganet.chi.il.us (Felix Jeske) writes:
  12.  
  13. >>     int i = 2;
  14. >>      i = ++i;
  15. >>   What should the value of i be?
  16. >>
  17. >>>3.
  18. >>
  19. >>BZZZT.
  20.  
  21. >Double BZZZT!
  22.  
  23. Triple BZZZT!  [I normally find that kind of thing quite rude, but
  24. I'll play when dealing with someone who has that kind of mindset.]
  25.  
  26. >The following:
  27.  
  28. >        i=++i;
  29.  
  30. >should be broken up like when taking operator precedence into account:
  31.  
  32. >        ++i;
  33. >        i=i;
  34.  
  35. Look in the ANSI Standard.  Show me where the sequence point is that
  36. lets you do the increment operator to 'i' first.  Note that '=' is NOT
  37. a sequence point.
  38.  
  39. >i is incremented (changing it's value to 3) and then is assigned to itself (a
  40. >no-op) so i=3 in the end.
  41.  
  42. Where have you BEEN for the periodic discussions of this one, son?  A
  43. bit late in the year for incoming Freshmen, isn't it?
  44.  
  45. >>
  46. >>       x = ++i;
  47. >>
  48. >>means that x is assigned the incremented value of i and
  49. >>i is incremented, it in no way requires an ordering of
  50. >>these two operations.
  51.  
  52. >Yes it does, the precedence of the = and prefix ++ operators defines the order
  53. >of these operations quite clearly.  Since a prefixed ++ has a higher precedence
  54. >(cf. K&R) than =, it (the ++) is performed first and the statement should be
  55. >broken up as follows:
  56.  
  57. >        ++i;
  58. >        x=i;
  59.  
  60. >No ambiguity whatsoever.
  61.  
  62. Wrong all the way.  See the ANSI C Standard.  Side effects are not
  63. evaluated based on precedence.  Increment and decrement operators have
  64. side effects.  Side effects are only guaranteed to have occurred after
  65. a sequence point.  The assignment operation is not a sequence point.
  66. Therefore, you have multiple references to 'i' in an expression
  67. involving side effects with no sequence point between the references.
  68.  
  69. Can you say 'undefined behaviour'?
  70.  
  71. -- 
  72. "Insisting on perfect safety is for people who don't have the balls to live
  73.  in the real world."   -- Mary Shafer, NASA Ames Dryden
  74. ------------------------------------------------------------------------------
  75. Fred.McCall@dseg.ti.com - I don't speak for others and they don't speak for me.
  76.