home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!crdgw1!rdsunx.crd.ge.com!bart!volpe
- From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
- Newsgroups: comp.lang.c
- Subject: Re: Testing competence of potential hires
- Message-ID: <1992Dec11.213235.24557@crd.ge.com>
- Date: 11 Dec 92 21:32:35 GMT
- References: <1g9pm5INNosg@almaak.usc.edu>
- Sender: volpe@bart (Christopher R Volpe)
- Reply-To: volpe@ausable.crd.ge.com
- Organization: GE Corporate Research & Development
- Lines: 84
- Nntp-Posting-Host: bart.crd.ge.com
-
- In article <1g9pm5INNosg@almaak.usc.edu>, ajayshah@almaak.usc.edu (Ajay Shah) writes:
- |>
- |> Write a test program which will tell you whether min is a true function,
- |> or a #define. You are not allowed to know anything about how your
- |> program is linked; you are only allowed to write this program, and
- |> see it's execution results.
- |>
- |> Answer 1:
- |> {
- |> int i, j, s;
- |> i=2; j=4;
- |> s = min((++i), j);
- |> printf("%d\n", s);
- |> }
- |>
- |> Answer 2: try compiling
- |> {
- |> char p,q,s;
- |> s = min(p, q);
- |> }
- |>
- |> A1. If it's written as #define min(a,b) a<b?a:b then ++i will get
- |> executed twice, giving 4. Otherwise the min will be 3.
-
- What if it's written as:
- #define min(a,b) \
- (__a = a, __b = b, (__a<__b?__a:__b))
-
- where __a and __b are file scope statics declared in min.h??
-
-
- |>
- |> A2: if this program compiles it's a hash-def, else it's a true function.
- |>
-
- Why? The chars will be promoted to int and the resulting int stored in
- a char. There's no reason why this won't compile if it's a function.
-
- My solution:
-
- #include <stdio.h>
- #include "min.h"
- int main()
- {
- #ifdef min
- printf("min is a macro\n");
- #else
- printf("min is a function\n");
- #endif
- return 0;
- }
-
-
-
- |>
- |> 5. After
- |> int i = 2;
- |> i = ++i;
- |> what does i equal?
- |>
- |> Answer: Nothing prevents the compiler from interpreting this as:
- |>
- |> i = i + 1;
- |> ++i;
- |>
- |> i.e., there is no reason the compiler can't decide to find what value
- |> is to be assigned to the i on the LHS side of the equation and only
- |> then look at the right hand side to increment i by 1.
-
- The example may be illegal, but your reasoning is wrong. The "++i"
- attempts to store 3 in i. The value of the "++i" expression is 3, regardless
- of when the side effect takes place. Thus, the effect of the "=" is to
- attempt to store 3 in i also. Both side effects attempt to store 3 in i.
- This may violate the standard, depending on the definition of "modified"
- in section 3.3, but I don't see why any reasonable implementation would
- not put 3 in i. Note this is different from "i = i++" (postfix), where the
- "++" wants to put 3 in i, but the "=" wants to put 2 (the value of the
- "i++" expression) in i.
-
- --
- ==================
- Chris Volpe
- G.E. Corporate R&D
- volpecr@crd.ge.com
-