function call in conditional operator?

B

bnp

Hi,

Is possible to use functions in conditional operator as ashown below.
.....
.....
int fun1()
{
// body
}

int fun2()
{
//body
}

....
main()
{...
int x;
x = (a>b)?fun1():fun2();
}
 
R

Régis Troadec

bnp said:
Hi,
Hi,

Is possible to use functions in conditional operator as ashown below.
....
....
int fun1()
{
// body
}

int fun2()
{
//body
}

...
main()
{...
int x;
x = (a>b)?fun1():fun2();

Yes,

it's semantically equivalent to:

if (a>b)
x = fun1();
else
x = fun2();

IMHO, I find that the conditional operator fits very well in this case.

Regis
 
C

Case

Kevin said:
Yes. And even more entertainingly, you can do this:

x = (a > b ? fun1 : fun2) (c, d, e);

Yes, of course! Never thought of stretching the meaning
of a function 'pointer' this far. Things like 5["Hello world"]
(as you know I'm sure) are entertaining too.

Kees
 
P

Paul D. Boyle

: In message <[email protected]>
: (e-mail address removed) (bnp) wrote:

:> Hi,
:>
:> Is possible to use functions in conditional operator as ashown below.

:> x = (a>b)?fun1():fun2();

: Yes. And even more entertainingly, you can do this:

: x = (a > b ? fun1 : fun2) (c, d, e);

Are you saying that if functions 'fun1' and 'fun2' have the same
prototypes and take (appropriately typed) arguments, 'c', 'd', and 'e',
that this would be equivalent to:

if ( a > b )
x = fun1( c, d, e );
else
x = fun2( c, d, e );

?

Paul
 
C

Case

Kevin said:
Yes. And even more entertainingly, you can do this:

x = (a > b ? fun1 : fun2) (c, d, e);

BTW what other seemingly strange standard C construction
do you know? I mentioned 5["fsdssdfssf"] which looks
very strange to most people even very experienced C
programmers. It would be fun to discuss this kind of
constructs.

Case
 
C

Case

Case said:
Kevin said:
Yes. And even more entertainingly, you can do this:

x = (a > b ? fun1 : fun2) (c, d, e);

BTW what other seemingly strange standard C constructions
do you know? I mentioned 5["fsdssdfssf"] which looks
very strange to most people

About the whole human race of course. But I meant C
programmers. :)

Kees
 
E

Eric Sosman

bnp said:
Hi,

Is possible to use functions in conditional operator as ashown below.
....
....
int fun1()
{
// body
}

int fun2()
{
//body
}

...
main()
{...
int x;
x = (a>b)?fun1():fun2();
}

Yes. You can even use `x = (a > b ? fun1 : fun2)();'
if you have sado-masochistic tendencies.
 
G

glen herrmannsfeldt

bnp wrote:

(snip)
Is possible to use functions in conditional operator as ashown below.
(snip)

int x;
x = (a>b)?fun1():fun2();

Not only that, you can even do

x=(a>b?fun1:fun2)();

assuming that fun1 and fun2 are declared as functions.
(It is more interesting if they have arguments.)


-- glen
 
P

pete

Paul said:
: In message <[email protected]>
: (e-mail address removed) (bnp) wrote:

:> Hi,
:>
:> Is possible to use functions in conditional operator as ashown below.

:> x = (a>b)?fun1():fun2();

: Yes. And even more entertainingly, you can do this:

: x = (a > b ? fun1 : fun2) (c, d, e);

Are you saying that if functions 'fun1' and 'fun2' have the same
prototypes and take (appropriately typed) arguments, 'c', 'd', and
'e',

It could happen. Take a look at this line of code

string = (digit == 0 ? sput_ip1 : sput_i)(integer / 10, string);

from

http://groups.google.com/[email protected]

sput_ip1 and sput_i, are functions.
 
K

Keith Thompson

Case said:
Kevin said:
Yes. And even more entertainingly, you can do this:
x = (a > b ? fun1 : fun2) (c, d, e);

BTW what other seemingly strange standard C construction
do you know? I mentioned 5["fsdssdfssf"] which looks
very strange to most people even very experienced C
programmers. It would be fun to discuss this kind of
constructs.

Google "Duff's Device".

You might also take a look at the past winners of the International
Obfuscated C Code Contest (google "IOCCC"). Please keep firmly in
mind that some of the earlier winners are non-portable, and that
*none* of them are examples of good C code (except for extremely and
deliberately perverse values of "good").
 
A

August Derleth

bnp wrote:

(snip)


Not only that, you can even do

x=(a>b?fun1:fun2)();

Yes, that is possible, now that I think of it. It could even be a rather
elegant solution to a problem.

It will, however, leave the wet-behind-the-ears maintenance programmer who
was raised on Java and Visual Basic scratching his head, as well as
sparking a Holy War with those who still believe in Structured Programming. ;)
 
C

Case

Keith said:
Case said:
Kevin said:
Yes. And even more entertainingly, you can do this:
x = (a > b ? fun1 : fun2) (c, d, e);

BTW what other seemingly strange standard C construction
do you know? I mentioned 5["fsdssdfssf"] which looks
very strange to most people even very experienced C
programmers. It would be fun to discuss this kind of
constructs.


Google "Duff's Device".

This one is really amazing! Thanks.
You might also take a look at the past winners of the International
Obfuscated C Code Contest (google "IOCCC"). Please keep firmly in
mind that some of the earlier winners are non-portable, and that
*none* of them are examples of good C code (except for extremely and
deliberately perverse values of "good").

I know IOCCC and own the book "Obfuscated C and Other Mysteries" by Don
Libes. That's real fun too indeed.

Kees
 
J

Joona I Palaste

Yes, that is possible, now that I think of it. It could even be a rather
elegant solution to a problem.
It will, however, leave the wet-behind-the-ears maintenance programmer who
was raised on Java and Visual Basic scratching his head, as well as
sparking a Holy War with those who still believe in Structured Programming. ;)

It's not so much a feature of C as a feature of first-class functions.
In other languages, like ML or Haskell, you can even manufacture new
functions at run-time and then call them.

For example:
mynewfunction :: int -> (int -> int)
mynewfunction x = \y.(x+y)

When called, for example, with an argument of 5, this function returns
a function that adds 5 to its argument. Can you do this in C?

It wouldn't be too much trouble, semantically, to add method references
to Java. How it's handled down at the implemenation level is anyone's
guess, though.
 
D

Dan Pop

In said:
Is possible to use functions in conditional operator as ashown below.
....
....
int fun1()
{
// body
}

int fun2()
{
//body
}

...
main()
{...
int x;
x = (a>b)?fun1():fun2();
}

In such cases, the best way of finding the answer is by trying to answer
another question: why not?

Dan
 
C

Chris Dollin

Case said:
Yes, of course! Never thought of stretching the meaning
of a function 'pointer' this far.

That's hardly "stretching", more a "perfectly routine use".

[And people think the Sapir-Whorf hypothesis is false ...]
 
G

glen herrmannsfeldt

August Derleth wrote:

(snip)
Yes, that is possible, now that I think of it. It could even be a rather
elegant solution to a problem.
It will, however, leave the wet-behind-the-ears maintenance programmer who
was raised on Java and Visual Basic scratching his head, as well as
sparking a Holy War with those who still believe in Structured Programming. ;)

Well, in Java you can do it with Object reference variables.

x=(a>b?string1:string2).equals("hi");

something like:

x=!strcmp((a>b?string1:string2),"hi");

With Java's reflection, you can probably do the equivalent of
function pointers in a conditional expression, though I won't try.

I don't know about VB at all.

-- glen
 
M

Malcolm

Case said:
BTW what other seemingly strange standard C construction
do you know? I mentioned 5["fsdssdfssf"] which looks
very strange to most people even very experienced C
programmers. It would be fun to discuss this kind of
constructs.
C allows a lot of this sort of thing in order to make the compiler's grammar
easier to specify.
The problem is that people then think it is fun to use such constructions in
production code.
 
M

Mitchell

Yes, of course! Never thought of stretching the meaning
of a function 'pointer' this far. Things like 5["Hello world"]
(as you know I'm sure) are entertaining too.

I'm a bit stumped here, how does 5("Hello world"); works?
#define 5(_a) printf("%s",_a) ?
 
J

Joona I Palaste

Mitchell said:
Yes, of course! Never thought of stretching the meaning
of a function 'pointer' this far. Things like 5["Hello world"]
(as you know I'm sure) are entertaining too.
I'm a bit stumped here, how does 5("Hello world"); works?
#define 5(_a) printf("%s",_a) ?

5("Hello world"); doesn't work. It's 5["Hello world"] with square
brackets instead of normal ones. It works because [] is actually a
commutative operator - a is the exact same thing as b[a].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top