nested conditional operator

J

junky_fellow

Hi guys,

I have a piece of code:

int func(int flag)
{
int number=1;
if (flag == 1)
return 0;
if(flag == 2)
if( number == 1)
return 0;
else
return number;
return(number);
}

I want to replace this function with a macro.

int main(void)
{
int result;
int flag = 0;
result = func(flag);
}

I tried using nested conditional operator convert the function to
macro, but end
up with some problems.

Can anybody please help me on this ??

thanks in advance for your help ....
 
C

Chris Dollin

I have a piece of code:

int func(int flag)
{
int number=1;
if (flag == 1)
return 0;
if(flag == 2)
if( number == 1)
return 0;
else
return number;
return(number);
}

/Really/ this code? Because the test for `number == 0` seems
a little superfluous.
I want to replace this function with a macro.
Why?

int main(void)
{
int result;
int flag = 0;
result = func(flag);
}

I tried using nested conditional operator convert the function to
macro, but end
up with some problems.

Can anybody please help me on this ??

You haven't shown us the code you had problems with, and you
haven't said what the problems were, so it's a bit tricky.

Remember that a conditional expression is an /expression/ and
hence <fx:sob/> can't include declarations.
 
P

pete

Hi guys,

I have a piece of code:

int func(int flag)
{
int number=1;
if (flag == 1)
return 0;
if(flag == 2)
if( number == 1)
return 0;
else
return number;
return(number);
}

I want to replace this function with a macro.

int main(void)
{
int result;
int flag = 0;
result = func(flag);
}

I tried using nested conditional operator convert the function to
macro, but end
up with some problems.

Can anybody please help me on this ??

#define FUNC(f) (f == 1 || f == 2 && number == 0 ? 0 : number)
 
C

Christopher Benson-Manica

#define FUNC(f) (f == 1 || f == 2 && number == 0 ? 0 : number)

I don't know if OP superseded a previous post, but based on what I can
see, the test should be number == 1, not 0.
 
A

Andrew Poelstra

#define FUNC(f) (f == 1 || f == 2 && number == 0 ? 0 : number)

Actually, this particular code guarantees that the number is one.
The macro could simply be:

#define FUNC(f) (f != 1 && f != 2)

You could add a ? 1 : 0 to that if you wanted.
 
J

junky_fellow

pete said:
#define FUNC(f) (f == 1 || f == 2 && number == 0 ? 0 : number)

Thanks pete for your help. Although I posted the modified and very
simplified code
but your reply did help me to solve the problem.
 
C

CBFalconer

I have a piece of code:

int func(int flag)
{
int number=1;
if (flag == 1)
return 0;
if(flag == 2)
if( number == 1)
return 0;
else
return number;
return(number);
}

I want to replace this function with a macro.

#define func(i) (!((1 == i) || (2 == i)))

and don't call it with i an expression with side effects.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
F

Fred Kleinschmidt

Christopher Benson-Manica said:
I don't know if OP superseded a previous post, but based on what I can
see, the test should be number == 1, not 0.
Also the above will not work for FUNC(a+b)
You need more parentheses to be safe:
#define FUNC(f) ((f) == 1 || ( f ) == 2 && number == 1 ? 0 : number)
 
K

Kenneth Brody

Fred said:
Also the above will not work for FUNC(a+b)
You need more parentheses to be safe:
#define FUNC(f) ((f) == 1 || ( f ) == 2 && number == 1 ? 0 : number)

Nor will these work with FUNC(x++).

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
O

Old Wolf

Andrew said:
Actually, this particular code guarantees that the number is one.

The OP is asking for a macro to replace the entirety of the function
"func"; I'm not sure how pete's macro is supposed to be used.
The macro could simply be:

#define FUNC(f) (f != 1 && f != 2)

#define FUNC(f) ( (f) != 1 && (f) != 2 )

I think we can do it without evaluating f twice (assuming that
f+1 does not cause an integer overflow):

#define FUNC(f) ( ((f) + 1) / 2 != 1 )
 
T

Thad Smith

I have a piece of code:

int func(int flag)
{
int number=1;
if (flag == 1)
return 0;
if(flag == 2)
if( number == 1)
return 0;
else
return number;
return(number);
}

number holds the value 1 throughout the function.
I want to replace this function with a macro.

int main(void)
{
int result;
int flag = 0;
result = func(flag);
}

#define func(flag) (((flag)==1 || (flag)==2)?0:1)

or

#define func(flag) (!((flag)==1 || (flag)==2))

or

#define func(flag) ((flag)!=1 && (flag)!=2)

or

#define func(flag) (!!(((unsigned)(flag)-1)&~1))

The last has the advantage of evaluating its argument only once.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top