-2

How could I achieve something like this ...

int main(void)
{
  if (f(x) == (a || b))
  {
     puts("Success");
  }
  return (0);
}

This would print Success if the return of f(x) is equal to a or b. I know it is possible to store it in a variable but my question is: "Could something like this be done by calling the f(x) function only once without using a variable?"

Edit 1: I'm not allowed to use the switch statement for this assignment

Edit 2: Could I set a range with only one expression like this?

if ( 2 < f(x) < 5)

Would this be valid (return type is int)?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

3 Answers3

2

how to test for multiple return values from a function called once without storing into a variable (?)

Not really, but with some restrictions let us abuse C and assume a, b and f() return a character.

1Form a character array made up of a and b and search it using memchr(). Inspired by @David C. Rankin (It does not store the result of f() in a variable, but does call a function)

int main(void) {
  //         v-------------v compound literal
  if (memchr((char [2]){a,b}, f(x), 2)) {
     puts("Success");
  } 
  return 0;
}

I see OP added "return type is int" - Oh well.


if ( 2 < f(x) < 5) is valid code, but is does not do what OP wants.

It is like if ( (2 < f(x)) < 5) which compares f(x) with 2 and results in 0 or 1, which is always less than 5.


Tough crowd tonight, so how about the below. Needs a bit of extension math for int overflow`, but is close.

abs(2*f(x) - (a+b)) == abs(a-b)

1 Not serious code suggestions for production code - use a temporary.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    We can at least make it fun and `srand (time(NULL));` then `if (memchr ("ab", f(rand()), 2)) puts ("Success"); else puts ("Failure");` where `int f (int x) { return x % 3 + 'a'; }` ---- `:)` – David C. Rankin Mar 10 '20 at 22:55
  • 1
    `memchr((char [2]){a,b}, f(x), 2)` for the love of God why would you *ever* suggest something like this................... – Marco Bonelli Mar 10 '20 at 23:28
  • @Marco Bonelli Why, because even though not practical, it is interesting to see how far one can go. How about `abs(2*f(x) - (a+b)) == abs(a-b)`? With `long long` extensions to handle overflow, that could work. But this is all in [fun](https://stackoverflow.com/help/how-to-answer). A temporary is the way to go. – chux - Reinstate Monica Mar 10 '20 at 23:35
  • @chux-ReinstateMonica this is a programming help Q&A website, not a code-golfing or obfuscation challenge... while I too find that funny I think you should make clear that this is not a serious answer. – Marco Bonelli Mar 10 '20 at 23:40
  • @MarcoBonelli Fair point. I was thinking the "let us abuse C" was enough of a hint as to "not a serious answer.". Perhaps too subtle. – chux - Reinstate Monica Mar 10 '20 at 23:44
  • It does not change too much - you have to save a & b or result of f(x). There is no way of avoiding it. memchr - saves both – 0___________ Mar 11 '20 at 00:21
  • I dunno, I kinda thought the compound-literal added a nice touch... – David C. Rankin Mar 11 '20 at 01:22
1

This can obviously be done using a switch statement. Another way would be calling a function returning true or false with the first function value as input, another way could be a jump table or even > or bit checking using binary operators depending on a and b values (very common for testing multiple bit flags at once).

But really you shouldn't care about using or not using a variable in such cases. Current compilers are quite good putting temporary variables like that in registers.

EDIT: given the constraints, the most likely solution is using some bit fu, but it fully depends of values of a and b and c, etc. The common way is using powers of two as values to check. Then you can check a set of values in only one operation.

exemple: a = 1, b = 2, c = 4

if (f(x) & (1+2+4)) {...}

checks if we have a or b or c or a superposition of these values.

kriss
  • 23,497
  • 17
  • 97
  • 116
0

C language does not such constructs. You need do save the result of the function or/and both a & b.

Of course you can:

int compare(int a, int b, int f)
{
    if(a == f || b == f) { puts("Success"); return 0;}
    return -1;
}

int f(int x)
{
    return x * x;;
}

int main()
{
    compare(5,8,f(3));
}

but of course it saves all the values as the functions parameters.

0___________
  • 60,014
  • 4
  • 34
  • 74