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();
} 30 2121
"bnp" <bh**********@yahoo.com> a écrit dans le message de
news:7d*************************@posting.google.co m... 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
In message <7d*************************@posting.google.com> bh**********@yahoo.com (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);
--
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/
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
Kevin Bracey <ke**********@tematic.com> wrote:
: In message <7d*************************@posting.google.com>
: bh**********@yahoo.com (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
--
Paul D. Boyle bo***@laue.chem.ncsu.edu
North Carolina State University http://www.xray.ncsu.edu
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
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():fun2(); }
Yes. You can even use `x = (a > b ? fun1 : fun2)();'
if you have sado-masochistic tendencies.
-- Er*********@sun.com
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
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
Paul D. Boyle wrote: Kevin Bracey <ke**********@tematic.com> wrote: : In message <7d*************************@posting.google.com> : bh**********@yahoo.com (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/groups?selm...mindspring.com
sput_ip1 and sput_i, are functions.
--
pete
Case <no@no.no> writes: 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.
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").
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
On Mon, 17 May 2004 18:40:49 +0000, glen herrmannsfeldt wrote: 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)();
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. ;)
--
yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
To email me, rot13 and convert spelled-out numbers to numeric form.
"Makes hackers smile" makes hackers smile.
Keith Thompson wrote: Case <no@no.no> writes:
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.
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
August Derleth <se*@sig.now> scribbled the following: On Mon, 17 May 2004 18:40:49 +0000, glen herrmannsfeldt wrote: 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. ;)
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.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Nothing lasts forever - so why not destroy it now?"
- Quake
In <7d*************************@posting.google.com> bh**********@yahoo.com (bnp) writes: 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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
August Derleth wrote:
(snip) 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. ;)
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
"Case" <no@no.no> wrote in message 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.
On Mon, 17 May 2004 18:17:05 +0200, Case <no@no.no> wrote: 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) ?
Mitchell <ch***************@inahatespamme.cohatespamm> scribbled the following: On Mon, 17 May 2004 18:17:05 +0200, Case <no@no.no> wrote:
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[b] is the exact same thing as b[a].
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Case <no@no.no> wrote in message news:<40*********************@news.xs4all.nl>... 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.
Why is the resultant deferenced type (char) and not some other
scalar type? k_*****@yahoo.com (kal) wrote: Case <no@no.no> wrote in message news:<40*********************@news.xs4all.nl>...
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.
Why is the resultant deferenced type (char) and not some other scalar type?
Because that's how the Standard defines it. Besides, what other type
would you have it be?
Richard
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote: "Case" <no@no.no> wrote in message
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.
Oddly enough, in this case the grammar wouldn't have been much easier if
this were disallowed. "The first expression shall have type 'pointer to
object type', the second expression shall have integral type" wouldn't
have been any simpler than the actual C89 sentence "One of the
expressions shall have type 'pointer to object type', the other
expression shall have integral type". It probably even would've made
compilers (very) slightly smaller.
Richard
Joona I Palaste <pa*****@cc.helsinki.fi> wrote: Mitchell <ch***************@inahatespamme.cohatespamm> scribbled the following: On Mon, 17 May 2004 18:17:05 +0200, Case <no@no.no> wrote:
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[b] is the exact same thing as b[a].
Or, to put it another way, the array subscript operator [] is
essentially "syntactic sugar" and a[b] is semantically equivalent
to (*((a)+(b))), which in turn obviously equals (*((b)+(a))),
because of the commutative nature of the binary + operator.
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
In <a5*************************@posting.google.com> k_*****@yahoo.com (kal) writes: Case <no@no.no> wrote in message news:<40*********************@news.xs4all.nl>...
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.
Why is the resultant deferenced type (char) and not some other scalar type?
Engage your brain and tell us what type can be expected from
"fsdssdfssf"[5]. Then explain why would you expect another type from
5["fsdssdfssf"]. I assume you have already read the FAQ...
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
"Case" <no@no.no> wrote in message
news:40*********************@news.xs4all.nl... Keith Thompson wrote: Case <no@no.no> writes:
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.
Google "Duff's Device".
This one is really amazing! Thanks.
send(to, from, count)
register short *to, *from;
register count;
{
register n=(count+7)/8;
switch(count%8){
case 0: do{ *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
}while(--n>0);
}
}
So basically at some count != 8 you end up with:
} while (--n>0);
And this is legal? What about the } with no opening {?
--
Mabden Da*****@cern.ch (Dan Pop) wrote in message news:<c8**********@sunnews.cern.ch>... Then explain why would you expect another type from 5["fsdssdfssf"].
Now it seems clear. I got muddled about 'left' and 'right' operands.
But when you phrase it as "one is a pointer type and the other an
integral type" it clarifies itself.
I assume you have already read the FAQ...
Haven't read it. In fact I wasn't planning to read it till I
came across references to it in a few posts. Those references
were direct and pertinent (references to 16.5 etc.) I think one
was yours (Dan Pop) and the names of the posters of the other
two I can't recall at the moment (one was about "if (2 == x) ..."
form.)
When I went and looked at it I was surprised to see how well
done it is. So, I have read SOME of it and am still reading
parts of it almost everday. Thanks for directing me there.
In <3a*****************@newssvr27.news.prodigy.com> "Mabden" <ma****@sbcglobal.net> writes: send(to, from, count) register short *to, *from; register count; { register n=(count+7)/8; switch(count%8){ case 0: do{ *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; }while(--n>0); } }
So basically at some count != 8 you end up with:
} while (--n>0);
And this is legal? What about the } with no opening {?
Good question. A strict reading of the standard makes it undefined
behaviour. This is not the intent of the authors, but they couldn't be
bothered to get it right.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In message <c8***********@sunnews.cern.ch> Da*****@cern.ch (Dan Pop) wrote: In <3a*****************@newssvr27.news.prodigy.com> "Mabden" <ma****@sbcglobal.net> writes:
And this is legal? What about the } with no opening {?
Good question. A strict reading of the standard makes it undefined behaviour. This is not the intent of the authors, but they couldn't be bothered to get it right.
Of course, they're still some way ahead of those who couldn't be bothered to
get involved in authoring it.
--
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/
Groovy hepcat Mitchell was jivin' on Wed, 19 May 2004 11:07:08 +0800
in comp.lang.c.
Re: function call in conditional operator?'s a cool scene! Dig it! On Mon, 17 May 2004 18:17:05 +0200, Case <no@no.no> wrote:
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?
It doesn't. But 5["Hello world"] does. It works like this:
1) "Hello world" is somewhat equivalent to
char somestring[] = {'H', 'e', 'l', 'l', 'o', ' ',
'w', 'o', 'r', 'l', 'd'};
2) In an expression, "Hello world"[5] is therefore somewhat equivalent
to somestring[5].
3) This is equivalent to *(somestring + 5).
4) That is equivalent to *(5 + somestring).
6) And that is equivalent to 5[somestring].
7) So, that is somewhat equivalent to 5["Hello world"].
Understand?
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
"glen herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote: Well, in Java you can do it with Object reference variables.
x=(a>b?string1:string2).equals("hi");
The concept works on C too, except that it's messy to include a reference to
the object as part of the method's arguments.
#include <string.h>
struct string {
int (*equals)(struct string *, const char *);
char *data;
};
int str_equals(struct string *this, const char *other)
{
return !strcmp(this->data, other);
}
int main(void)
{
struct string string1 = {str_equals, "Hello"};
struct string string2 = {str_equals, "World"};
int x, a = 1, b = 2;
x = ((a > b) ? string1 : string2).equals(((a > b) ? &string1 : &string2),
"hi");
return 0;
}
--
Simon. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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" */
}
|
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...
|
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...
|
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 :...
|
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;
|
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...
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |