buls@dataio.UUCP (Rick Buls) (04/18/85)
/* ** Ok netland its C quiz time. Not to be found in ** "The C Puzzle Book" by Alan Feuer. ** ** Below is a C program that tests the famous foo. ** ** The question: what does the following program print? ** ** Spoiler: The answer is NOT "foo returns 5" ** */ #define dum(x) /* empty debug macro */ main() { printf("foo returns %d\n",foo()); /* print answer */ } foo() { int a,b,*p= &a; /* p points to a */ b=10; *p=2; /* set a to 2 */ a=b/*p; /* div b by contents of pointer(ie a) */ dum(a); return(a); }
rkl@ahuta.UUCP (k.laux) (04/18/85)
REFERENCES: <646@dataio.UUCP> Pretty slick, although misleading. This little program most graphically demonstrates that one should ALWAYS PUT SPACES AROUND OPERATORS. BTW, the answer is that foo prints 10 because the '/*' in 'a=b/*p;' is interpreted as the start of a comment; the preprocessor is the culprit because it looks for comments to strip them out before the compilation phase (ie order of precedence does not apply). R. Kevin Laux Software Vendor Tech Support ATTIS Lincroft ahuta!rkl
jans@mako.UUCP (Jan Steinman) (04/19/85)
In article <646@dataio.UUCP> buls@dataio.UUCP (Rick Buls) writes: >foo() >{ > int a,b,*p= &a; /* p points to a */ > Automatics cannot be initialized this way, and I don't believe the comment would be correct if they could. Perhaps: int a, b, *p; p = &a; is what was intended? Perhaps I'm missing something? -- :::::: Jan Steinman Box 1000, MS 61-161 (w)503/685-2843 :::::: :::::: tektronix!tekecs!jans Wilsonville, OR 97070 (h)503/657-7703 ::::::
lspirkov@udenva.UUCP (Goldilocks) (04/22/85)
In article <> buls@dataio.UUCP (Rick Buls) writes: > >#define dum(x) /* empty debug macro */ > >main() >{ > printf("foo returns %d\n",foo()); /* print answer */ >} > >foo() >{ > int a,b,*p= &a; /* p points to a */ > > b=10; > *p=2; /* set a to 2 */ > a=b/*p; /* div b by contents of pointer(ie a) */ ^^ > dum(a); > return(a); >} on the line where you have: a=b/*p; /* div b by ... */ the /* between the b and the p is the beginning of the comment, not a div (/) and then a pointer (*). and the answer i got was 10. and that's b/dum(a); (anyone know why dum(a) has the value 1???) Goldi
lspirkov@udenva.UUCP (Goldilocks) (04/22/85)
>on the line where you have: > a=b/*p; /* div b by ... */ > >the /* between the b and the p is the beginning of the comment, not >a div (/) and then a pointer (*). and the answer i got was 10. and that's >b/dum(a); (anyone know why dum(a) has the value 1???) > > Goldi wait, wait. don't answer yet. dum(a) doesn't have the value 1 because it's not really a=b/dum(a); it's really a=b dum(a); and since dum(x) is a blank macro, the equation is a=b; that's why the answer is 10. Goldi ps: i would have retracted my first message but i don't know how nor do i want to read the manual.
rkl@ahuta.UUCP (k.laux) (04/22/85)
REFERENCES: <717@mako.UUCP> I believe you'll find that 1) int a, b, *p = &a; and 2) int a, b, *p; p = &a; ARE equivalent. I took the code exactly as offered from the net and cc'd it with no problems. You will find that in either case the compiler generates code to load the address of 'a' and store it in 'p'. 1) is more compact, 2) is more explicit. R. Kevin Laux Software Vendor Tech Support ATTIS Lincroft ahuta!rkl (soon to be mtuxo!rkl) 201-576-3610