Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!mit-eddie!genrad!decvax!ima!cfisun!lakart!dg From: dg@lakart.UUCP (David Goodenough) Newsgroups: comp.lang.c Subject: Re: Value, value, who's got the value? Message-ID: <521@lakart.UUCP> Date: 28 Apr 89 16:13:04 GMT References: <1044@itivax.iti.org> Organization: Lakart Corporation, Newton, MA Lines: 74 From article <1044@itivax.iti.org>, by scs@vax3.iti.org (Steve Simmons): ] Consider the following program: ] ] int func1() ] { ] int b ; ] b = 2 ; ] } ] ] int func2() ] { ] int c = 3 ; ] c ; ] } ] ] main() ] { ] int a = 1 ; ] printf( "Value of a is %d\n", a ) ; ] a = func1() ; ] printf( "Value of a is %d\n", a ) ; ] a = func2() ; ] printf( "Value of a is %d\n", a ) ; ] } ] ] Compile and run this on a UNIX-PC (system V) under standard cc or ] with gcc, and the result is: ] Value of a is 1 ] Value of a is 2 ] Value of a is 3 ] ] On BSD43. with standard cc or gcc, the result is ] Value of a is 1 ] Value of a is 0 ] Value of a is 0 ] ] Several questions: why does the OS make a difference; It's not the OS, it's the compiler. Since you didn't say what func1() and func2() return explicitly, they return whatever happens to be in registers at the time. On a 68020, generally the return value comes back in d0, on a 286 / 386 machine I'd guess it'd be in ax, although what you'd do with a long I'm not sure. ] why does ] System V get it 'right' (even tho the code is wrong); Both get it right, since the return value of a function without a return expr ; is undefined. ] why do ] none of these flag func2 as having a syntax error? Because it doesn't have one. statement ::= ...... | expr ; | ......... expr ::= ....... | variable | ......... Hence int i; i; is syntactically correct. -- dg@lakart.UUCP - David Goodenough +---+ IHS | +-+-+ ....... !harvard!xait!lakart!dg +-+-+ | AKA: dg%lakart.uucp@xait.xerox.com +---+