user1539348
user1539348

Reputation: 513

if statement in C & assignment without a double call

Would it be possible to implement an if that checks for -1 and if not negative -1 than assign the value. But without having to call the function twice? or saving the return value to a local variable. I know this is possible in assembly, but is there a c implementation?

int i, x = -10;
if( func1(x) != -1) i = func1(x);

Upvotes: 1

Views: 153

Answers (4)

autistic
autistic

Reputation: 15642

It depends whether or not func1 generates any side-effects. Consider rand(), or getchar() as examples. Calling these functions twice in a row might result in different return values, because they generate side effects; rand() changes the seed, and getchar() consumes a character from stdin. That is, rand() == rand() will usually1 evaluate to false, and getchar() == getchar() can't be predicted reliably. Supposing func1 were to generate a side-effect, the return value might differ for consecutive calls with the same input, and hence func1(x) == func1(x) might evaluate to false.

If func1 doesn't generate any side-effect, and the output is consistent based solely on the input, then I fail to see why you wouldn't settle with int i = func1(x);, and base logic on whether or not i == -1. Writing the least repetitive code results in greater legibility and maintainability. If you're concerned about the efficiency of this, don't be. Your compiler is most likely smart enough to eliminate dead code, so it'll do a good job at transforming this into something fairly efficient.

1. ... at least in any sane standard library implementation.

Upvotes: 1

Josh Petitt
Josh Petitt

Reputation: 9579

The best implementation I could think of would be:

int i = 0; // initialize to something
const int x = -10;
const int y = func1(x);

if (y != -1)
{
  i = y;
}

The const would let the compiler to any optimizations that it thinks is best (perhaps inline func1). Notice that func is only called once, which is probably best. The const y would also allow y to be kept in a register (which it would need to be anyway in order to perform the if). If you wanted to give more of a suggestion, you could do:

register const int y = func1(x);

However, the compiler is not required to honor your register keyword suggestion, so its probably best to leave it out.

EDIT BASED ON INSPIRATION FROM BRIAN'S ANSWER:

int i = ((func1(x) + 1) ?:0) - 1;

BTW, I probably wouldn't suggest using this, but it does answer the question. This is based on the SO question here. To me, I'm still confused as to the why for the question, it seems like more of a puzzle or job interview question than something that would be encountered in a "real" program? I'd certainly like to hear why this would be needed.

Upvotes: 0

Brian Cain
Brian Cain

Reputation: 14619

saving the return value to a local variable

In my experience, avoiding local variables is rarely worth the clarity forfeited. Most compilers (most of the time) can often avoid the corresponding load/stores and just use registers for those locals. So don't avoid it, embrace it! The maintainer's sanity that gets preserved just might be your own.

I know this is possible in assembly, but is there a c implementation?

If it turns out your case is one where assembly is actually appropriate, make a declaration in a header file and link against the assembly routine.

Suggestion:

const int x = -10;
const int y = func1(x);
const int i = y != -1
            ? y
            : 0 /* You didn't really want an uninitialized value here, right? */ ;

Upvotes: 2

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29213

int c;
if((c = func1(x)) != -1) i = c;

Upvotes: 0

Related Questions