[comp.lang.c] Returning a value from ?:

DAVE%UWF.BITNET@wiscvm.wisc.EDU (05/28/87)

All,
    Well, my recent probs with printf are solved, by linking with LCM
ahead of LC, as suggested.  Thanks.
    Next in line, I just (yesterday) got a copy of Turbo C, and was
trying out some old programs on it.  It works mostly like a champ, but
I ran across a small bug.  It goes like this:

     (f(a) * f(p) > 0) ? (a = p) : (b = p);

with the function f() being defined elsewhere, and with all things being
defined as double.  I got it to work by splitting up the ? : up into
an if ... else, but it worked w/o hitch under Lattice 3.0.  Is the above
statement not valid C code?  Should the result be put into a variable of
some sort?  I am aware of the philosophy that all expressions should
return a value, and that the value returned should be referenced.  So
is Turbo just enforcing that, or should Borland have allowed it to pass
with a warning?  (No warning was generated, but the results of the code
was strange until I cleaned it up.)

Thanx,

Dave (w/o a clue) Jaquay (DAVE@UWF.BITNET)

kurt@hi.UUCP (Kurt Zeilenga) (05/30/87)

In article <7555@brl-adm.ARPA> DAVE%UWF.BITNET@wiscvm.wisc.EDU writes:
>
>All,
>    Well, my recent probs with printf are solved, by linking with LCM
>ahead of LC, as suggested.  Thanks.
>    Next in line, I just (yesterday) got a copy of Turbo C, and was
>trying out some old programs on it.  It works mostly like a champ, but
>I ran across a small bug.  It goes like this:
>
>     (f(a) * f(p) > 0) ? (a = p) : (b = p);
>
>with the function f() being defined elsewhere, and with all things being
>defined as double.  I got it to work by splitting up the ? : up into
>an if ... else, but it worked w/o hitch under Lattice 3.0.  Is the above

By "worked" do you mean compiled or running with "proper" results.

>statement not valid C code?

The above statement is valid.  However, beware of side effects and order
of evaulation, you are not guaranteed the order of the evaulation of
f(a) * f(p).  Ie: some compilers may do f(a) first and then f(p), others
may do f(p) first.  K&R allows the compiler to rewrite such expressions.
Could this be the root of the evil?

Hope this helps....
-- 
	Kurt Zeilenga	 (zeilenga@hc.dspo.gov)		I want my talk.flame!

	"So long, Mom, I'm off to kill a commie..."

keesan@cc5.bbn.com.UUCP (06/02/87)

In article <7555@brl-adm.ARPA> DAVE%UWF.BITNET@wiscvm.wisc.EDU writes:
>
>     (f(a) * f(p) > 0) ? (a = p) : (b = p);
>
>Is the above
>statement not valid C code?  Should the result be put into a variable of
>some sort?  I am aware of the philosophy that all expressions should
>return a value, and that the value returned should be referenced.  So
>is Turbo just enforcing that, or should Borland have allowed it to pass
>with a warning?

Sounds like your problem was simply a bug in the compiler.  The above
expression is perfectly valid, and the expression (a = p) returns a value
just as much as any conditional expression.  You can't reference the value of
every expression, nor is there any reason to.
-- 
Morris M. Keesan
keesan@cci.bbn.com
{harvard,decvax,ihnp4,etc.}!bbnccv!keesan

markg@amd.UUCP (Mark Gorlinsky) (06/02/87)

In article <7555@brl-adm.ARPA> DAVE%UWF.BITNET@wiscvm.wisc.EDU writes:
>    Next in line, I just (yesterday) got a copy of Turbo C, and was
>trying out some old programs on it.  It works mostly like a champ, but
>I ran across a small bug.  It goes like this:
>
>     (f(a) * f(p) > 0) ? (a = p) : (b = p);
>
>...  I got it to work by splitting up the ? : up into
>an if ... else, but it worked w/o hitch under Lattice 3.0.  Is the above
>statement not valid C code? 

This is certainly proper use of the ternary operator "?:".

I tried to make my TURBO C fail using your code fragment.  It DIDN'T fail!!
You must have something else wrong in you code, or you need to give us a better
sample of the code!  

Did you try to isolate this statement?  What memory model were you using?  What
options were set?  Please give more information!
 




-- 
 Mark Gorlinsky - AMD Processor Products Division/APPS SQA
 UUCP: {decwrl,ihnp4,allegra}!amd!markg
 AT&T: (408) 982-7811
 DISCLAIMER: My opinions are mine, not my employers. 

ftw@datacube.UUCP (06/10/87)

Looks to me like valid C.  If you could tell us what DID happen (i.e., what
got assigned to "p"), it might help.  If Borland's intent is to force you
to assign the result of the entire expression to something, then it should
have generated some diagnostic (as you said).  Producing "funny" code is
not acceptable.


			Farrell

ftw@datacube.UUCP (06/10/87)

Oops, I meant to ask: "What did "p" get assigned to, if anything?"