home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Bug_Fixes / Net.v7bugs / 0104 < prev    next >
Encoding:
Text File  |  1982-05-04  |  740 b   |  32 lines

  1. Autzoo.1633
  2. net.bugs.v7
  3. utzoo!henry
  4. Tue May  4 22:02:15 1982
  5. overflow in C revisited
  6. Very entertaining things can happen with integer overflow in C.  This
  7. isn't really a bug, except that (as reported some months ago) the
  8. manual should say the effects of overflow are "undefined" instead of
  9. saying that it is "ignored".  Consider the following:
  10.  
  11. -----
  12. /*
  13.  * Arithmetic in the presence of overflow is context-dependent in
  14.  * very strange ways.
  15.  */
  16.  
  17. unsigned int compile = (56*1024)/64;
  18.  
  19. main()
  20. {
  21.     printf("%u == %u\n", compile, (56*1024)/64);
  22.     printf("%u == %u == %u (!!)\n", compile, 57734/64, (56*1024)/64);
  23. }
  24. -----
  25.  
  26. Can you guess what this prints?  Like this:
  27.  
  28.     65408 == 65408
  29.     65408 == 0 == 902 (!!)
  30.  
  31. Yes, friends, it's context-dependent!
  32.