home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12319 < prev    next >
Encoding:
Internet Message Format  |  1992-08-13  |  3.5 KB

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem on getting video ram data
  5. Message-ID: <3227@dozo.and.nl>
  6. Date: 14 Aug 92 09:48:06 GMT
  7. References: <1992Aug12.170428.1@cucs17.cs.cuhk.hk>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 88
  10.  
  11. In article <1992Aug12.170428.1@cucs17.cs.cuhk.hk> lou1347@cucs17.cs.cuhk.hk 
  12. originally asked why this doesn't work:
  13.  
  14.   code=(*video++)+(*video++ & 8)?256:0;
  15.  
  16. Someone else replied:
  17.  
  18. |According to "The C Pocket Reference" by Herbert Schildt,
  19. |Osborne McGraw-Hill, ?: operator's presedence is lower than
  20. |+'s, so code is either 256 or 0.
  21.  
  22. And the answer to that is:
  23.  
  24. |Thanks, but I still in trouble.  I wrote another test program like that:
  25. |
  26. |code=(*video++)+((*video++ & 8)?256:0);
  27. |
  28. |The bracket is added.  I expect the result to be 10.  But It give 276!!
  29. |(If I split it into two lines, the result will be 10).
  30. |
  31. |I think the problem is that the operand after '+' do first so that the 
  32. |first *video got 20 and second *video got 10 which give the result 276.
  33. |
  34. |But I know that in C,'+' sign will do from left to right.  ( I also write a
  35. |program to test it and it is true )  So what is the result?
  36.  
  37. I can predict you, that result is implementation dependent.
  38. The addition and subtraction operators are just left-associative.
  39. This just means that a+b-c will be evaluated as (a+b)-c, but it 
  40. says nothing about the order in which the individual operands are
  41. evaluated. 
  42.  
  43. The problem with your original code is, you're trying to squeeze too
  44. much functionality in one expression. The standard says:
  45.  
  46. ANSI-C 2.1.2.3 Program execution
  47.  
  48. [ ... ] At certain specified points in the execution sequence called
  49. _sequence points_, all side effects of previous evaluations shall be
  50. complete and no side effects of subsequent evaluations shall have
  51. take place.
  52.  
  53. In a following example, they describe your problem:
  54.  
  55. The grouping of an expression does not completely determine its 
  56. evaluation. In the following fragment
  57.  
  58. sum= sum * 10 - '0' + (*p++= getchar());
  59.  
  60. the expression statement is grouped as if it were written as
  61.  
  62. sum= (((sum * 10) - '0') + ((*(p++)) = (getchar())));
  63.  
  64. but the actual increment of p can occur at any time between the previous
  65. sequence point and the next sequence point (the ;) and the call to 
  66. getchar can occur at any point prior to the need of its returned value.
  67.  
  68. [ end quote ]
  69.  
  70. Your code fragement `*video++' occurs even _twice_ between two sequence
  71. points. The postfix ++ operator is a hidden assignment. About assignments,
  72. the standard says the following:
  73.  
  74. 3.3.16 Assignment operators
  75.  
  76. [ ... ] The side effect of updating the stored value of the left operand
  77. shall occur between the previous and the next sequence point.
  78.  
  79. So, your compiler is free to fetch the value of the video variable,
  80. register the fact somewhere that this variable needs to be incremented
  81. later and goes on with the show. This increment is not necessarily done
  82. right after fetching the old value, it can be done just when the sequence
  83. point is reached (the `;' here).
  84.  
  85. You've solved your problem yourself already: you've split the assignment
  86. expression in two, and that was the only correct thing you could do.
  87.  
  88. moral of the story: don't write code as if you were participating in the
  89. Obfuscated C Code Contest. It doesn't make your code more efficient,
  90. it just shrinks your source files a bit. Let the compiler do the hard
  91. work of optimization, it can do a much better job than you and I can ever
  92. accomplish.
  93.  
  94. I hope this clarifies things a bit,
  95.  
  96. kind regards,
  97.  
  98. Jos aka jos@and.nl
  99.