home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- 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
- From: david@oahu.cs.ucla.edu (David Dantowitz)
- Subject: Result of y = (y++) % 3;
- Message-ID: <1993Jan24.153017.11068@cs.ucla.edu>
- Sender: usenet@cs.ucla.edu (Mr Usenet)
- Nntp-Posting-Host: oahu.cs.ucla.edu
- Organization: UCLA, Computer Science Department
- Date: Sun, 24 Jan 93 15:30:17 GMT
- Lines: 42
-
-
-
- A beginner C programmer wrote the following:
-
- main()
- {
- int i, y;
-
- y = 0;
-
- for (i=0; i < 10; i++)
- {
- y = (y++) % 3;
- printf("%d\n", y);
- }
- }
-
- When they really intended y = (y+1) % 3; (Yes it is faster to avoid the
- '%' operator all together.)
-
- Most compilers produce code that does the following:
-
- temp = y;
- y += 1;
- y = temp % 3;
-
- Thus, 0 is always printed.
-
- The IBM's RISC 6000 compiler produces 1, 2, 3, 1, 2, 3.
-
- So, even though 3/4 tested compilers produce 0, I'd like to
- get some second opinions...
-
- Please reply by email and I'll summarize.
-
- David
-
- --
- David Dantowitz
- david@cs.ucla.edu
-
- Singing Barbershop when I'm not doing parallel simulation
-