home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / amiga / programm / 17222 < prev    next >
Encoding:
Text File  |  1992-12-11  |  2.9 KB  |  87 lines

  1. Newsgroups: comp.sys.amiga.programmer
  2. Path: sparky!uunet!ornl!utkcs2!darwin.sura.net!spool.mu.edu!agate!stanford.edu!rock!concert!sas!mozart.unx.sas.com!walker
  3. From: walker@twix.unx.sas.com (Doug Walker)
  4. Subject: Re: SAS6.1 users: please read
  5. Originator: walker@twix.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <Bz3z3A.13E@unx.sas.com>
  8. Date: Fri, 11 Dec 1992 18:52:22 GMT
  9. References:  <ByM64K.6z2@fc.hp.com>
  10. Nntp-Posting-Host: twix.unx.sas.com
  11. Organization: SAS Institute Inc.
  12. Lines: 73
  13.  
  14.  
  15. In article <ByM64K.6z2@fc.hp.com>, koren@fc.hp.com (Steve Koren) writes:
  16. |> Consider this code:
  17. |> 
  18. |> /***********************************************************************
  19. |>  **   sc test1.c
  20. |>  **   slink from lib:c.o test1.o to test1 lib lib:sc.lib
  21. |>  ***********************************************************************/
  22. |> 
  23. |> # include <stdio.h>
  24. |> # include <string.h>
  25. |> 
  26. |> main(int argc, char **argv) {
  27. |>    char *str = "abc";
  28. |>    int  num = 7;
  29. |> 
  30. |>    if ((strlen(str) - num) > 0) {
  31. |>       printf("Ooops!\n");
  32. |>    }
  33. |> 
  34. |>    exit(0);
  35. |> }
  36. |> 
  37. |> Even though one would expect
  38. |>    if ((strlen(str) - num) > 0) {
  39. |> 
  40. |> in cases where strlen(str)=3 and num=7 to fail (ie, 3-7 is -4,
  41. |> and -4 is NOT greater than 0 last time I checked), the if
  42. |> statement passes instead.
  43.  
  44. This is not a bug, this is correct behavior.  It's a confusing 
  45. point in the ANSI C standard that I've explained here a couple of
  46. times before, in slightly different terms.
  47.  
  48. strlen() is defined by the standard as returning a value of type
  49. "size_t", which must be an unsigned integer value.  In our case,
  50. it is "unsigned int".
  51.  
  52. The variable "num" is of type "int", of course, as is the constant
  53. zero.  Therefore, the subtraction in the condition of the if 
  54. statement is
  55.  
  56.    "unsigned int" - "int"
  57.  
  58. The ANSI expression rules state that the types of the expression
  59. must be promoted to the "widest" type in the expression, or "int",
  60. whichever is smaller.  i.e. in the above expression, the second
  61. "int" gets converted to "unsigned int" and the result of the
  62. expression is "unsigned int".
  63.  
  64. Since the result of the expression is unsigned, it is impossible
  65. for the condition to be negative.  Thus, the subtraction results
  66. in an underflow and the result is a large positive number.
  67.  
  68. |> I hope this helps someone else avoid the code problems I was
  69. |> having.  I'm sure they'll fix this for 6.2.  SASC continues,
  70. |> IMHO, to be the best development environment for the Amiga bar
  71. |> none.
  72. |> 
  73. |>   - steve
  74.  
  75. Thanks for the compliment.  Unfortunately, we can't change this
  76. without violating the ANSI (and now ISO) standard for C expressions.
  77.  
  78. -- 
  79.   *****
  80. =*|_o_o|\\=====Doug Walker, Software Distiller====== BBS: (919)460-7430 =
  81.  *|. o.| ||                                          1200/2400/9600 Dual
  82.   | o  |//     For all you do, this bug's for you!
  83.   ====== 
  84. usenet: walker@unx.sas.com                            bix: djwalker 
  85. Any opinions expressed are mine, not those of SAS Institute, Inc.
  86.  
  87.