C语言 C中的三元运算符和返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3566413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ternary operators and Return in C
提问by nullpointerexception
Why can't we use return keyword inside ternary operators in C, like this: sum > 0 ? return 1 : return 0;
为什么我们不能在 C 中的三元运算符中使用 return 关键字,例如: sum > 0 ?返回1:返回0;
回答by Ignacio Vazquez-Abrams
returnis a statement. Statements cannot be used inside expressions in that manner.
return是一个声明。语句不能以这种方式在表达式中使用。
回答by Wolph
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:
或者正如 DrDipShit 指出的那样:
return sum > 0;
回答by Steve Summit
The ternary operator deals in expressions, but returnis a statement.
三元运算符处理表达式,但它return是一个语句。
The syntax of the returnstatement is
return语句的语法是
returnexpr;
return表达式;
The syntax of the ternary conditional operator is
三元条件运算符的语法是
expr1?expr2:expr3
expr1 ?expr2 :expr3
So you can plug in an invocation of the ternary operator as the exprin a returnstatement. But you cannot plug in a returnstatement as expr2or expr3of a ternary operator.
所以,你可以在三元运算符作为一个调用插件EXPR的return声明。但是您不能将return语句插入为三元运算符的expr2或expr3。
The ternary expression acts a lot like an ifstatement, but it is not an exact replacement for an ifstatement. If you want to write
三元表达式的作用很像一个if语句,但它不是if语句的精确替代品。如果你想写
if(sum > 0)
return 1;
else return 0;
you can write it as a true ifstatement, but you can't convert it to using ? :without rearranging it a little, as we've seen here.
你可以把它写成一个真实的if语句,但你不能把它转换为 using? :而不稍微重新排列它,正如我们在这里看到的。
回答by Steve Summit
Because returnis a statement, not an expression. You can't do int a = return 1;either.
因为return是语句,而不是表达式。你也做不到int a = return 1;。
回答by ksrao
See the syntax of a ternary operator is
请参阅三元运算符的语法是
expr1 ? expr2: expr3;
where expr1, expr2, expr3are expressions;
其中expr1, expr2,expr3是表达式;
The operator ?:works as follows
expr1is evaluated first if it is true expr2is evaluated otherwise expr3is evaluated.
运算符的?:工作方式如下,
expr1如果为真expr2则先求值,否则expr3求值。
hence in expressions the returnstatement can not be used in C-language.
因此,在表达式中,不能在 C 语言中使用return语句。
回答by Achal
The returnstatement is used for returning from a function, you can't use inside ternary operator.
该return语句用于从 a 返回function,不能在三元运算符内部使用。
(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, expr3are expressions and returnis a statement, not an expression.
其中expr1, expr2,expr3是表达式并且return是语句,而不是表达式。
回答by Some_Tasty_Donuts
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;
仅通过查看语法,您就应该知道不能在表达式中使用语句。您可以通过以下方式实现您想要的:return sum > 0 ? 1 : 0;

