home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20144 < prev    next >
Encoding:
Text File  |  1993-01-24  |  1.1 KB  |  54 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!udel!darwin.sura.net!zaphod.mps.ohio-state.edu!usc!news.service.uci.edu!ucivax!ucla-cs!oahu.cs.ucla.edu!david
  3. From: david@oahu.cs.ucla.edu (David Dantowitz)
  4. Subject: Result of y = (y++) % 3;
  5. Message-ID: <1993Jan24.153017.11068@cs.ucla.edu>
  6. Sender: usenet@cs.ucla.edu (Mr Usenet)
  7. Nntp-Posting-Host: oahu.cs.ucla.edu
  8. Organization: UCLA, Computer Science Department
  9. Date: Sun, 24 Jan 93 15:30:17 GMT
  10. Lines: 42
  11.  
  12.  
  13.  
  14. A beginner C programmer wrote the following:
  15.  
  16. main()
  17. {
  18.   int i, y;
  19.  
  20.   y = 0;
  21.  
  22.   for (i=0; i < 10; i++)
  23.     {
  24.       y = (y++) % 3;
  25.       printf("%d\n", y);
  26.     }
  27. }
  28.  
  29. When they really intended y = (y+1) % 3;  (Yes it is faster to avoid the
  30. '%' operator all together.)
  31.  
  32. Most compilers produce code that does the following:
  33.  
  34. temp = y;
  35. y += 1;
  36. y = temp % 3;
  37.  
  38. Thus, 0 is always printed.  
  39.  
  40. The IBM's RISC 6000 compiler produces 1, 2, 3, 1, 2, 3.
  41.  
  42. So, even though 3/4 tested compilers produce 0, I'd like to
  43. get some second opinions...
  44.  
  45. Please reply by email and I'll summarize.
  46.  
  47. David
  48.  
  49. -- 
  50. David Dantowitz
  51. david@cs.ucla.edu
  52.  
  53. Singing Barbershop when I'm not doing parallel simulation
  54.