Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!amdcad!crackle!tim From: tim@crackle.amd.com (Tim Olson) Newsgroups: comp.lang.c Subject: Re: Value, value, who's got the value? Message-ID: <25429@amdcad.AMD.COM> Date: 27 Apr 89 16:34:43 GMT References: <1044@itivax.iti.org> Sender: news@amdcad.AMD.COM Reply-To: tim@amd.com (Tim Olson) Organization: Advanced Micro Devices, Inc. Sunnyvale CA Lines: 72 Summary: Expires: Sender: Followup-To: In article <1044@itivax.iti.org> scs@vax3.iti.org (Steve Simmons) writes: | | 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; The OS doesn't cause the difference -- the compiler does. Since func1 and func2 have no return statements, the value returned is whatever last happend to be placed in the return-value location. On your UNIX-PC, this is probably the same register as the first "temp" register that gets assigned during expression evaluation. On the BSD machine, it isn't. | why does | System V get it 'right' (even tho the code is wrong); The compilers on that machine under that OS just happened to do what you thought should happen. The code is wrong, because there are no explicit return statements. | why do | none of these flag func2 as having a syntax error? Because it isn't a syntax error -- it is a legal (although useless) expression. Remember that in C, assignments aren't statements -- they are expressions. Here is what our local C compiler had to say about your program: w "t.c",L1/C9: func1: Function has no return statement. w "t.c",L12/C9: c: Expression has no side-effects. w "t.c",L8/C9: func2: Function has no return statement. w "t.c",L18/C9: printf: Function called but not defined. -- Tim Olson Advanced Micro Devices (tim@amd.com)