linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Re: ternary operator
  2005-06-28 17:49 ` Steve Graegert
@ 2005-06-28 17:15   ` Luiz Fernando Capitulino
  2005-06-28 18:24     ` Steve Graegert
  2005-06-28 18:24   ` Vadiraj
  1 sibling, 1 reply; 7+ messages in thread
From: Luiz Fernando Capitulino @ 2005-06-28 17:15 UTC (permalink / raw)
  To: Steve Graegert; +Cc: Vadiraj, linux-c-programming

Steve Graegert wrote:

> Just take a look at our discussion about implicit return values on
> int- and void-valued functions a week or two ago.  If no return value
> is given explicitly it is undefined and in most cases not zero,
> therfore not yielding false.  It's chosen randomly.

Yes, but note that this happens because the default return value
for functions is 'int'. So, if you change func2()'s prototype to
return 'void', the compiler will give you a warning.

PS: This is not the case for the arguments, in that case func2() it's
able to accept anything (IIRC).

-- 
Luiz Fernando N. Capitulino

^ permalink raw reply	[flat|nested] 7+ messages in thread

* ternary operator
@ 2005-06-28 17:22 Vadiraj
  2005-06-28 17:44 ` Ron Michael Khu
  2005-06-28 17:49 ` Steve Graegert
  0 siblings, 2 replies; 7+ messages in thread
From: Vadiraj @ 2005-06-28 17:22 UTC (permalink / raw)
  To: linux-c-programming

Hi List,

 I'm confused with the behavior of this program..

func4()
{
    return 3;
}
func3()
{
    return 2;
}
func2()
{
}

func1()
{
    return(func2()?func3():func4()) ;
}
int main()
{
     
     printf("%d\n",func1() ) ;                       // this prints 3
     printf("%d\n",func2()?func3():func4()) ;  // this prints 2
}

 I dont understand whats making the two statements to behave differently.



-- 
cheers,
Vadi

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ternary operator
  2005-06-28 17:22 ternary operator Vadiraj
@ 2005-06-28 17:44 ` Ron Michael Khu
  2005-06-28 17:49 ` Steve Graegert
  1 sibling, 0 replies; 7+ messages in thread
From: Ron Michael Khu @ 2005-06-28 17:44 UTC (permalink / raw)
  To: Vadiraj; +Cc: linux-c-programming

i dont know the reason but the problem could be shrink down to

func2()
{
}

func1()
{
  printf( "%d\n", func2() );
}

int main()
{
  printf( "%d\n", func2() );
  printf("%d\n",func1() ) ;
}

u will notice that the return value from func2(which does not specify 
any explicit return value)
differs when called from main() and from func1()..

this is not a problem about the ternary operator..
this is an issue about the use of implicit/explicit return values...
why are u not  making use of explicit return? and explicit use of int as 
the return type in the
function declarations??


Vadiraj wrote:

>Hi List,
>
> I'm confused with the behavior of this program..
>
>func4()
>{
>    return 3;
>}
>func3()
>{
>    return 2;
>}
>func2()
>{
>}
>
>func1()
>{
>    return(func2()?func3():func4()) ;
>}
>int main()
>{
>     
>     printf("%d\n",func1() ) ;                       // this prints 3
>     printf("%d\n",func2()?func3():func4()) ;  // this prints 2
>}
>
> I dont understand whats making the two statements to behave differently.
>
>
>
>  
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ternary operator
  2005-06-28 17:22 ternary operator Vadiraj
  2005-06-28 17:44 ` Ron Michael Khu
@ 2005-06-28 17:49 ` Steve Graegert
  2005-06-28 17:15   ` Luiz Fernando Capitulino
  2005-06-28 18:24   ` Vadiraj
  1 sibling, 2 replies; 7+ messages in thread
From: Steve Graegert @ 2005-06-28 17:49 UTC (permalink / raw)
  To: Vadiraj; +Cc: linux-c-programming

On 6/28/05, Vadiraj <vadiraj.cs@gmail.com> wrote:
> Hi List,
> 
>  I'm confused with the behavior of this program..
> 
> func4()
> {
>     return 3;
> }
> func3()
> {
>     return 2;
> }
> func2()
> {
> }
> 
> func1()
> {
>     return(func2()?func3():func4()) ;
> }
> int main()
> {
> 
>      printf("%d\n",func1() ) ;                       // this prints 3
>      printf("%d\n",func2()?func3():func4()) ;  // this prints 2
> }
> 
>  I dont understand whats making the two statements to behave differently.

I am not able to recreate this behaviour, since both printf()s give
"2" on my console and this is exactly what I expected to take place. 
Just take a look at our discussion about implicit return values on
int- and void-valued functions a week or two ago.  If no return value
is given explicitly it is undefined and in most cases not zero,
therfore not yielding false.  It's chosen randomly.

In this case the :? operator says:  if func2() returns true, call
func3() else call func4().  In most cases func2() is true.  Nothing
magic here.

-- 
Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ternary operator
  2005-06-28 17:15   ` Luiz Fernando Capitulino
@ 2005-06-28 18:24     ` Steve Graegert
  0 siblings, 0 replies; 7+ messages in thread
From: Steve Graegert @ 2005-06-28 18:24 UTC (permalink / raw)
  To: Luiz Fernando Capitulino; +Cc: linux-c-programming

On 6/28/05, Luiz Fernando Capitulino <lcapitulino@conectiva.com.br> wrote:
> Steve Graegert wrote:
> 
> > Just take a look at our discussion about implicit return values on
> > int- and void-valued functions a week or two ago.  If no return value
> > is given explicitly it is undefined and in most cases not zero,
> > therfore not yielding false.  It's chosen randomly.
> 
> Yes, but note that this happens because the default return value
> for functions is 'int'. So, if you change func2()'s prototype to
> return 'void', the compiler will give you a warning.

Exactly what has been discussed by Glynn, me and others a couple of
days ago.  The reference to void-valued functions was made to reflect
the nature of the discussion that has taken place before; it has no
significance in this thread.

> PS: This is not the case for the arguments, in that case func2() it's
> able to accept anything (IIRC).

As stated in the answer to James Colannino's question on function prototyping.

Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ternary operator
  2005-06-28 17:49 ` Steve Graegert
  2005-06-28 17:15   ` Luiz Fernando Capitulino
@ 2005-06-28 18:24   ` Vadiraj
  2005-06-28 18:47     ` Steve Graegert
  1 sibling, 1 reply; 7+ messages in thread
From: Vadiraj @ 2005-06-28 18:24 UTC (permalink / raw)
  To: Steve Graegert; +Cc: linux-c-programming

Hi,

On 6/28/05, Steve Graegert <graegerts@gmail.com> wrote:
> On 6/28/05, Vadiraj <vadiraj.cs@gmail.com> wrote:
> > Hi List,
> >
> >  I'm confused with the behavior of this program..
> >
> > func4()
> > {
> >     return 3;
> > }
> > func3()
> > {
> >     return 2;
> > }
> > func2()
> > {
> > }
> >
> > func1()
> > {
> >     return(func2()?func3():func4()) ;
> > }
> > int main()
> > {
> >
> >      printf("%d\n",func1() ) ;                       // this prints 3
> >      printf("%d\n",func2()?func3():func4()) ;  // this prints 2
> > }
> >
> >  I dont understand whats making the two statements to behave differently.
> 
> I am not able to recreate this behaviour, since both printf()s give
> "2" on my console and this is exactly what I expected to take place.
> Just take a look at our discussion about implicit return values on
> int- and void-valued functions a week or two ago.  If no return value
> is given explicitly it is undefined and in most cases not zero,
> therfore not yielding false.  It's chosen randomly.


> 
> In this case the :? operator says:  if func2() returns true, call
> func3() else call func4().  In most cases func2() is true.  Nothing
> magic here.

 Yes nothing magic here. I did get different values with few changes.
As far as I understand return values are stored in ax register. In this
program since func2() does not set AX it should get the value present at
that time.
  Most of the case it would be the return value from the function called before.
(may be or may not). Thats what I noticed when the code modified like this.

    func4() ;
    printf("%d\n", func2()) ; // prints 3
    func3() ;
    printf("%d\n", func2()) ; // prints 2

 Please add clarity to this if necessary. Thank you very much.
  

-- 
cheers,
Vadi

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: ternary operator
  2005-06-28 18:24   ` Vadiraj
@ 2005-06-28 18:47     ` Steve Graegert
  0 siblings, 0 replies; 7+ messages in thread
From: Steve Graegert @ 2005-06-28 18:47 UTC (permalink / raw)
  To: Vadiraj; +Cc: linux-c-programming

On 6/28/05, Vadiraj <vadiraj.cs@gmail.com> wrote:
> Hi,
> 
> On 6/28/05, Steve Graegert <graegerts@gmail.com> wrote:
> > On 6/28/05, Vadiraj <vadiraj.cs@gmail.com> wrote:
> > > Hi List,
> > >
> > >  I'm confused with the behavior of this program..
> > >
> > > func4()
> > > {
> > >     return 3;
> > > }
> > > func3()
> > > {
> > >     return 2;
> > > }
> > > func2()
> > > {
> > > }
> > >
> > > func1()
> > > {
> > >     return(func2()?func3():func4()) ;
> > > }
> > > int main()
> > > {
> > >
> > >      printf("%d\n",func1() ) ;                       // this prints 3
> > >      printf("%d\n",func2()?func3():func4()) ;  // this prints 2
> > > }
> > >
> > >  I dont understand whats making the two statements to behave differently.
> >
> > I am not able to recreate this behaviour, since both printf()s give
> > "2" on my console and this is exactly what I expected to take place.
> > Just take a look at our discussion about implicit return values on
> > int- and void-valued functions a week or two ago.  If no return value
> > is given explicitly it is undefined and in most cases not zero,
> > therfore not yielding false.  It's chosen randomly.
> 
> >
> > In this case the :? operator says:  if func2() returns true, call
> > func3() else call func4().  In most cases func2() is true.  Nothing
> > magic here.
> 
>  Yes nothing magic here. I did get different values with few changes.
> As far as I understand return values are stored in ax register. In this
> program since func2() does not set AX it should get the value present at
> that time.

AFAIK Return values of type int and pointers are usually stored in EAX
(AX is a 16 bit register), other types like long long may be stored in
EDX:EAX with the most and least significant words in EDX and EAX.

If no return type is given, the value found in EAX is returned
implicitly but this value often is, as you indicated, an artefact of
prior operations.  Nevertheless, a value is returned and some other
operations are performed as with int-valued functions to prevent
things like stack corruption and the like.

BTW: I am not an Intel guy, I'm running Alpha and SPARC Linux.  Please
correct me if I am wrong.

>   Most of the case it would be the return value from the function called before.
> (may be or may not). Thats what I noticed when the code modified like this.
> 
>     func4() ;
>     printf("%d\n", func2()) ; // prints 3
>     func3() ;
>     printf("%d\n", func2()) ; // prints 2
> 

This may indeed be true, but since func2() does not return an int, the
actual return value is an artifact from a prior call to func4() and
func3().  So yes, you are right and everything works as it should. 
When using the ternary operator you're adding some sort of complexety
to the calling sequence with the result that the EAX register may be
used for other purposes, thus holding obviously random values.

Kind Regards

    \Steve

--

Steve Graegert <graegerts@gmail.com> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176)  21 24 88 69
Office: +49 (9131) 71 26 40 9

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2005-06-28 18:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-28 17:22 ternary operator Vadiraj
2005-06-28 17:44 ` Ron Michael Khu
2005-06-28 17:49 ` Steve Graegert
2005-06-28 17:15   ` Luiz Fernando Capitulino
2005-06-28 18:24     ` Steve Graegert
2005-06-28 18:24   ` Vadiraj
2005-06-28 18:47     ` Steve Graegert

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).