From: Vadiraj <vadiraj.cs@gmail.com>
To: Steve Graegert <graegerts@gmail.com>
Cc: linux-c-programming <linux-c-programming@vger.kernel.org>
Subject: Re: ternary operator
Date: Tue, 28 Jun 2005 23:54:58 +0530 [thread overview]
Message-ID: <6eee1c40506281124101fe259@mail.gmail.com> (raw)
In-Reply-To: <6a00c8d50506281049335e427d@mail.gmail.com>
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
next prev parent reply other threads:[~2005-06-28 18:24 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2005-06-28 18:47 ` Steve Graegert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6eee1c40506281124101fe259@mail.gmail.com \
--to=vadiraj.cs@gmail.com \
--cc=graegerts@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).