home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18223 < prev    next >
Encoding:
Text File  |  1992-12-11  |  2.8 KB  |  98 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!crdgw1!rdsunx.crd.ge.com!bart!volpe
  2. From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Testing competence of potential hires
  5. Message-ID: <1992Dec11.213235.24557@crd.ge.com>
  6. Date: 11 Dec 92 21:32:35 GMT
  7. References: <1g9pm5INNosg@almaak.usc.edu>
  8. Sender: volpe@bart (Christopher R Volpe)
  9. Reply-To: volpe@ausable.crd.ge.com
  10. Organization: GE Corporate Research & Development
  11. Lines: 84
  12. Nntp-Posting-Host: bart.crd.ge.com
  13.  
  14. In article <1g9pm5INNosg@almaak.usc.edu>, ajayshah@almaak.usc.edu (Ajay Shah) writes:
  15. |> 
  16. |>    Write a test program which will tell you whether min is a true function,
  17. |>    or a #define.  You are not allowed to know anything about how your
  18. |>    program is linked; you are only allowed to write this program, and
  19. |>    see it's execution results.
  20. |> 
  21. |> Answer 1:
  22. |>    {
  23. |>         int i, j, s;
  24. |>         i=2; j=4;
  25. |>         s = min((++i), j);
  26. |>         printf("%d\n", s);
  27. |>    }
  28. |> 
  29. |> Answer 2: try compiling
  30. |>    { 
  31. |>         char p,q,s;
  32. |>         s = min(p, q);
  33. |>    }
  34. |> 
  35. |> A1. If it's written as #define min(a,b) a<b?a:b then ++i will get
  36. |> executed twice, giving 4.  Otherwise the min will be 3.
  37.  
  38. What if it's written as:
  39. #define min(a,b) \
  40.    (__a = a, __b = b, (__a<__b?__a:__b))
  41.  
  42. where __a and __b are file scope statics declared in min.h??
  43.  
  44.  
  45. |> 
  46. |> A2: if this program compiles it's a hash-def, else it's a true function.
  47. |> 
  48.  
  49. Why? The chars will be promoted to int and the resulting int stored in
  50. a char. There's no reason why this won't compile if it's a function.
  51.  
  52. My solution:
  53.  
  54. #include <stdio.h>
  55. #include "min.h"
  56. int main()
  57. {
  58. #ifdef min
  59.   printf("min is a macro\n");
  60. #else
  61.   printf("min is a function\n");
  62. #endif
  63.   return 0;
  64. }
  65.  
  66.  
  67.  
  68. |> 
  69. |> 5. After
  70. |>     int i = 2;
  71. |>     i = ++i;
  72. |>    what does i equal?
  73. |> 
  74. |> Answer: Nothing prevents the compiler from interpreting this as:
  75. |> 
  76. |>      i = i + 1;
  77. |>      ++i;
  78. |> 
  79. |> i.e., there is no reason the compiler can't decide to find what value
  80. |> is to be assigned to the i on the LHS side of the equation and only
  81. |> then look at the right hand side to increment i by 1.
  82.  
  83. The example may be illegal, but your reasoning is wrong. The "++i" 
  84. attempts to store 3 in i. The value of the "++i" expression is 3, regardless
  85. of when the side effect takes place. Thus, the effect of the "=" is to
  86. attempt to store 3 in i also. Both side effects attempt to store 3 in i.
  87. This may violate the standard, depending on the definition of "modified"
  88. in section 3.3, but I don't see why any reasonable implementation would
  89. not put 3 in i. Note this is different from "i = i++" (postfix), where the
  90. "++" wants to put 3 in i, but the "=" wants to put 2 (the value of the
  91. "i++" expression) in i.
  92.  
  93. -- 
  94. ==================
  95. Chris Volpe
  96. G.E. Corporate R&D
  97. volpecr@crd.ge.com
  98.