Ternary operators and Return in C

前端 未结 7 1207
一向
一向 2020-12-02 23:23

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

sum > 0 ? return 1 : return 0;
7条回答
  •  感情败类
    2020-12-02 23:59

    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.

提交回复
热议问题