home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3194 < prev    next >
Encoding:
Internet Message Format  |  1992-12-12  |  2.0 KB

  1. Path: sparky!uunet!sdrc!thor!scjones
  2. From: scjones@thor.sdrc.com (Larry Jones)
  3. Newsgroups: comp.std.c
  4. Subject: Re: float to cond conversions
  5. Message-ID: <2388@sdrc.COM>
  6. Date: 12 Dec 92 20:11:49 GMT
  7. References: <1992Dec09.011729.20954@Veritas.COM>
  8. Sender: news@sdrc.COM
  9. Lines: 44
  10.  
  11. In article <1992Dec09.011729.20954@Veritas.COM>, joshua@Veritas.COM (Joshua Levy) writes:
  12. > Consider the following code:
  13. >    int same(int b) { return b ; }
  14. >    if (X)
  15. >    if (same(!!(X)))
  16. >    if (same((int)(X))))
  17. >    X can be any legal C code including something like "f == 4.0"
  18. >    where f is a float or a double.
  19. > If I have an ANSI C compiler, are either of the second two if statements
  20. > identical in behavior to the first if statement?
  21.  
  22. Yes, the second one is.
  23.  
  24. First, note that your same() function is, at best, a long-winded way to
  25. write an (int) cast and, at worst, a noop.  Thus, the second and third
  26. if statements are equivalent to:
  27.     if ((int)!!(X))
  28.     if ((int)(int)(X))
  29. Since the result of the ! operator is already an int and you already had
  30. an explicit (int) cast in the third if statement, same() is a complete
  31. noop in both of the cases which then degerate to:
  32.     if (!!(X))
  33.     if ((int)(X))
  34.  
  35. Now, let's write out the implicit comparisons with 0 explicitly:
  36.     if ((X) != 0)
  37.     if (!((X) == 0))  => if ((X) != 0)
  38.     if ((int)(X) != 0)
  39.  
  40. It's now obvious that the first and second statements are identical and
  41. the third satement is also identical *if X has an integral value*.  If X
  42. has a non-integral value like 0.1, the first two statements will be true
  43. but the third will be false.
  44.  
  45. > I'm also interested in knowing if either of the second two if statements
  46. > will have different behavior in any common K&R C compiler.
  47.  
  48. No, but a K&R compiler will not automatically convert the argument to
  49. same() to int the way an ANSI compiler will (given a prototype).
  50. ----
  51. Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
  52. larry.jones@sdrc.com  or  ...uunet!sdrc!larry.jones
  53. I sure wish I could get my hands on some REAL dynamite. -- Calvin
  54.