473,770 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function call in conditional operator?

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():fu n2();
}
Nov 14 '05 #1
30 2172

"bnp" <bh**********@y ahoo.com> a écrit dans le message de
news:7d******** *************** **@posting.goog le.com...
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():fu n2();


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

Nov 14 '05 #2
In message <7d************ *************@p osting.google.c om>
bh**********@ya hoo.com (bnp) wrote:
Hi,

Is possible to use functions in conditional operator as ashown below. x = (a>b)?fun1():fu n2();


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

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

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #3
Kevin Bracey wrote:
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

Nov 14 '05 #4
Kevin Bracey <ke**********@t ematic.com> wrote:
: In message <7d************ *************@p osting.google.c om>
: bh**********@ya hoo.com (bnp) wrote:

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

:> x = (a>b)?fun1():fu n2();

: 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

--
Paul D. Boyle
bo***@laue.chem .ncsu.edu
North Carolina State University
http://www.xray.ncsu.edu
Nov 14 '05 #5
Kevin Bracey wrote:
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

Nov 14 '05 #6
bnp wrote:
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():fu n2();
}


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

--
Er*********@sun .com

Nov 14 '05 #7
Case wrote:
Kevin Bracey wrote:
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

Nov 14 '05 #8
bnp wrote:

(snip)
Is possible to use functions in conditional operator as ashown below.
(snip)
int x;
x = (a>b)?fun1():fu n2();


Not only that, you can even do

x=(a>b?fun1:fun 2)();

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

Nov 14 '05 #9
Paul D. Boyle wrote:

Kevin Bracey <ke**********@t ematic.com> wrote:
: In message <7d************ *************@p osting.google.c om>
: bh**********@ya hoo.com (bnp) wrote:

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

:> x = (a>b)?fun1():fu n2();

: 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/groups?selm...mindspring.com

sput_ip1 and sput_i, are functions.

--
pete
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
5961
by: Ken VdB | last post by:
Hi everyone, Is there a reason why the Mid() function only works in one direction in VBScript? This code works in VB6 but not in VBScript? Is there a way around it? I am trying to create an ASP page which produces a fixed width text file for import into a third party legacy application which I don't have control over. --code sample-- Dim strSomeString
6
3666
by: Mahesh Tomar | last post by:
Please see the code below :- void func() { unsigned char x,y,z=1; (z==1) ? (x) : (y) = 1; /* Compiles OK */ ((z==1) ? (x) : (y)) = 1; /* Compiler generates an error "Variable expected" */ }
27
2489
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to a pointer? #include <stdio.h> /* declares a function which takes an argument that is a...
9
2029
by: Netocrat | last post by:
Any comments on the correctness of the statements 1, 2a, 2b, 3 and 4 in the code below? If they are correct, then the definition of an object as well as that of an lvalue is broken in C99 by the following reasoning: foo() does not return an object, so the return of foo() conceptually is not stored, yet we are able to obtain a pointer to one of its member elements, therefore it must be stored, therefore foo() does return an object,...
6
12142
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null : DateTime.Now; } When the condition is false, I want to return null. If true, I want to return the current date/time.
5
2904
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
15
1806
by: Nicholas M. Makin | last post by:
I was just thinking that I understood the conditional operator when I coded the following expecting it to fail: int a= 10, b= 20, c= 0; ((a < b) ? a : b) = c; // a=0 a=20; b= 10; ((a < b) ? a : b) = c; //b=0 Now since the expresion 5 = c is not valid I did not expect the above to work. But it does work. Why? I thought that the ?: operator would evaluate
10
3619
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a cast if necessary but preferably to one of the /right/ function pointer
13
1557
by: Neal Becker | last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is y = some thing or other if x else something_else When scanning this my eye tends to see the first phrase and only later notice that it's conditioned on x (or maybe not notice at all!). Particularly if 'some thing or other' is long or complicated.
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.