0

My purpose is trying to write a function as concise and short as possible.

int func(void)
{
    int a;
    return (
        a = 42,
        a++,
        if (a > 42) a *= -1,
        a);
}

I was expecting to return a -43. Instead I've got a compilation error.

Pengchai
  • 50
  • 2
  • 7
Viodid
  • 11
  • 3
  • 2
    How about just `return a;` after everything? – dbush Feb 11 '23 at 03:25
  • 6
    Concise and short would be `return -43;` – user3386109 Feb 11 '23 at 03:27
  • 1
    Have a look at the `?:` operator. You can't have an `if` inside the return statement. – Déjà vu Feb 11 '23 at 03:40
  • What you have inside `return( ... )` is a comma operator. Expressions such as `a = 42` and `a++` are valid expressions that can be used with a comma operator; but `if (a>42) ... ` is a *statement*, and cannot be used in this way. – printf Feb 11 '23 at 03:54
  • You may also want to take a look at [statement expressions](https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html), which is a gcc extension to standard C, but [is also supported by many other compilers](https://stackoverflow.com/q/6440021/12149471). – Andreas Wenzel Feb 11 '23 at 11:41
  • What error did you get? – Michael M. Feb 12 '23 at 16:21

3 Answers3

6

My purpose is trying to write a function as concise and short as possible.

int a;

return (
   a = 42,
   a++,
   if (a > 42) a *= -1,
   a);

Can simply be rewritten as:

return -43;

Or if that's too short for your liking, then you're looking for the conditional operator (colloquially referred to as the ternary operator), which has the form:

/* if a is logically true (does not evaluate to zero) 
*  then evaluate expression b, otherwise 
*  evaluate expression c
*/
a ? b : c;

So the return statement can be rewritten as:

a = 42;
return ++a > 42 ? -a : a;

As of your objective, then there's no merit to it. You should not write clever code. It harms readability and maintainability. (Although in this case, it doesn't)

Remember:

  1. Simplicity is the ultimate sophistication.Leonardo da Vinci
  2. Any fool can write code that a computer can understand. Good programmers write code that humans can understand.Martin Fowler
  3. Everything should be made as simple as possible — but no simpler.Albert Einstein (attributed).¹

[1] — credit: @SteveSummit

Harith
  • 4,663
  • 1
  • 5
  • 20
1

If your goal is writing the function don't write it in the main function. write it outside the main and call the function in main The cause of your error is because of your statement expression there is an explanation here https://stackoverflow.com/a/45653540/14308832 So I guess you want to take a as parameter otherwise you can assign a as 42 in func

int func(int a){
    a++;
    return (a > 42 ? a *= -1 : a);
}

int main(void)
{
    printf("%d",func(42));
}
Pengchai
  • 50
  • 2
  • 7
  • 1
    the main function is just for demonstration purposes, my point was about what can it be done inside a return statement. Thank you anyway! – Viodid Feb 11 '23 at 04:11
0

Have you tried this?

int main(void)
{
    int a;

    return (a = 42, a++, a > 42 ? a = a * -1 : a);
}
  • You *can* do this, but if you don't like being shunned by your co-workers for writing unmaintainable code, you should not do this. – tadman Feb 11 '23 at 03:39
  • Wait, why does your code outputs 213? – Viodid Feb 11 '23 at 03:56
  • @viodid: main() has to return an integer between 0 and 255, so -43 is not going to work. But it's not a coincidence that 213+43=256. Read up on 2's complement. – rici Feb 11 '23 at 15:21