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
30 2177
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_Keit h) 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():fu n2();
Not only that, you can even do
x=(a>b?fun1:fun 2)();
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:fun 2)();
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.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Nothing lasts forever - so why not destroy it now?"
- Quake
In <7d************ *************@p osting.google.c om> bh**********@ya hoo.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():fu n2(); }
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:f un2)();
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************ ***@inahatespam me.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.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ 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 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
|
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 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...
|
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,...
|
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.
| |
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 : 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
|
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
|
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.
|
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...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |