29

Why can't we use return keyword inside ternary operators in C, like this:

sum > 0 ? return 1 : return 0;
7
  • 21
    What's wrong with return sum > 0 ? 1 : 0;? Aug 25, 2010 at 13:42
  • 2
    Or, for that matter, if (sum > 0) return 1 ; else return 0 ; Aug 25, 2010 at 13:46
  • 14
    Or for that matter return sum > 0; ?
    – CB Bailey
    Aug 25, 2010 at 13:47
  • 3
    Because a ? b : c is an expression that contains expressions, not statements. And return d is a statement, not an expression. Jun 27, 2018 at 13:07
  • 3
    Because it is an operator, not an if statement.
    – Lundin
    Jun 27, 2018 at 13:07

8 Answers 8

43

return is a statement. Statements cannot be used inside expressions in that manner.

37

Because a ternary operation is an expression and you can't use statements in expresssions.

You can easily use a ternary operator in a return though.

return sum > 0 ? 1 : 0;

Or as DrDipShit pointed out:

return sum > 0;
2
  • 11
    of just: return sum > 0; which works out as returning 1 or 0 anyway. Aug 25, 2010 at 13:47
  • @DrDipShit: very true, in this case that works just as well :)
    – Wolph
    Aug 25, 2010 at 15:03
15

The ternary operator deals in expressions, but return is a statement.

The syntax of the return statement is

return expr ;

The syntax of the ternary conditional operator is

expr1 ? expr2 : expr3

So you can plug in an invocation of the ternary operator as the expr in a return statement. But you cannot plug in a return statement as expr2 or expr3 of a ternary operator.

The ternary expression acts a lot like an if statement, but it is not an exact replacement for an if statement. If you want to write

if(sum > 0)
     return 1;
else return 0;

you can write it as a true if statement, but you can't convert it to using ? : without rearranging it a little, as we've seen here.

0
9

Because return is a statement, not an expression. You can't do int a = return 1; either.

2

See the syntax of a ternary operator is

expr1 ? expr2: expr3;

where expr1, expr2, expr3 are expressions;

The operator ?: works as follows expr1 is evaluated first if it is true expr2 is evaluated otherwise expr3 is evaluated.

hence in expressions the return statement can not be used in C-language.

0
2

The return statement is used for returning from a function, you can't use inside ternary operator.

 (1==1)? return 1 : return 0; /* can't use return inside ternary operator */

you can make it like

return (1==1) ? 1 : 0;

The syntax of a ternary operator follows as

expr1 ? expr2 : expr3;

where expr1, expr2, expr3 are expressions and return is a statement, not an expression.

1

You can use gcc's/clang's statement expressions feature.

#include <stdio.h>

#define discard(value) ({return value; value;})

int foo(int a) {
        int b = a%2 ?: discard(0);
        return b*a;
}

int main(int argc, char argv) {
        printf("foo(%d) = %d;\n", argc, foo(argc));
        return foo(argc);
}

Results are:

$ ./bar 
foo(1) = 1;
$ ./bar 2
foo(2) = 0;
$ ./bar 2 3
foo(3) = 3;
$ ./bar 2 3 4
foo(4) = 0;
$ ./bar 2 3 4 5
foo(5) = 5;
$ ./bar 2 3 4 5 6
foo(6) = 0;
$ ./bar 2 3 4 5 6 7
foo(7) = 7;
$ ./bar 2 3 4 5 6 7 8
foo(8) = 0;
$ ./bar 2 3 4 5 6 7 8 9
foo(9) = 9;
$ ./bar 2 3 4 5 6 7 8 9 10
foo(10) = 0;
0

Just by looking at the syntax you should know that an statement cannot be used in an expression. What you want can be achived by doing:return sum > 0 ? 1 : 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.