Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected expression before return

Tags:

c

the following c statement is not passing through compiler .error being "expected expression before return".

           int max( int a,int b)
            {
                 a>b?return a:return b;
            }

and yeah ,i know i can write this for finding max as

            return a>b?a: b;

which is quite okay and will run perfectly. but my question is what is exact problem with the first code.why cant we use return in ternary opoerator,although we can use function call quite easily over there?

THANKS in advance!!!

like image 373
mawia Avatar asked Dec 29 '25 16:12

mawia


2 Answers

The C grammar says that the things after the '?' and the ':' must be expressions - return is not an expression, it is a statement.

The operands of ternary ?: are expressions. A return statement is a statement, not an expression.

like image 37
Richard Pennington Avatar answered Dec 31 '25 07:12

Richard Pennington