QuickC
Scott Wilson
swilson%thetone at Sun.COM
Sat Aug 27 06:01:01 AEST 1988
In article <8808261424.AA11504 at ucbvax.Berkeley.EDU> TURGUT at TREARN.BITNET (Turgut Kalfaoglu) writes:
>main()
>{
> int a,v;
> a = 2;
> v = square(a);
> printf("%d\n",v);
>}
>
>square(num)
>int num;
>{
> num = num*num;
>}
>WHY does it work?
I get the same behavior on a Sun running 3.5 (but not 4.0). The reason
it works is that the scratch register used for the multiplication happens
to be the same register that is used to return the function's value (in
my case d0). The reason that adding the assignment to dummy didn't change
anything is that this most likely didn't involve any registers, the value
was stored directly in memory since dummy is on the stack.
Under SunOS 4.0 this gives the answer 0. For some reason all functions
without an explicit return statement return 0. I think it has something
to do with compatibility with 4.3 BSD where return(x) in main() is the
same as exit(x). Since main() often ends without a return (or exit()
being called) it could cause problems if a random register value were used
for the exit code. So, main() without a return returns zero which is
a "successfull" termination in UNIX.
--
Scott Wilson arpa: swilson at sun.com
Sun Microsystems uucp: ...!sun!swilson
Mt. View, CA
More information about the Comp.lang.c
mailing list