在同一个表达式中返回两次语句 [英] Return statement twice in the same expression

查看:72
本文介绍了在同一个表达式中返回两次语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写的:


#include< stdio.h>


int foo(int);


int main(无效){

int a = 3;

foo(a);

}


int foo(int n){

n> 10?返回1:返回0;

}


此代码产生编译错误,但如果我写:


return n> 10? 1:0;


相反,它有效。为什么?

标准对此有何评价?


TIA

解决方案
" Nerox" <是ne **** @ gmail.com>写道:

int foo(int n){
n> 10?返回1:返回0;
}




`return''不是表达式。这是一份声明。你不能这样使用



-

它不会是一个新的C标准,如果它并没有给静态这个词带来新的含义。

- C99上的彼得·希巴赫


Nerox写道:

如果我写:

#include< stdio.h>

int foo(int );

int main(void){
int a = 3;
foo(a);
}

int foo( int n){
n> 10?返回1:返回0;
}
此代码产生编译错误,但如果我写:

返回n> 10? 1:0;

相反,它有效。为什么?
标准对此有何看法?




三元运算符(< expr1>?< expr2>:< expr3>)是一个操作员,而不是
a控制结构。 [1]

三元运算符中涉及的所有表达式都必须产生一个值。

语句Return x不会产生一个值,实际上它会向调用者函数返回

控制,所以没有人返回一个值

来。这就是n>的原因。 10?返回1:返回0给出了
编译错误。


返回n> 10? 1:0,OTH是完全合法的。三元运算符

产生一个传递给返回的值。


BTW,在你的代码中它将完全等价,并且更加清晰。 >
只需写:


返回n> 10;


[1]它可以用作控制结构。像这样的句子:

n> 10? a = 1:a = 0;

完全合法,但是做法不好。这是因为一个分配确实产生了一个值(值为

分配)。


> int foo(int n){

n> 10?返回1:返回0;
}
此代码产生编译错误,但如果我写:

返回n> 10? 1:0;

相反,它有效。为什么?


您不能在表达式中放置* ANY * return语句。

但是,您可以在返回语句中放置表达式。

标准对此有何评价?




返回语句不是表达式,因此它不能是

与?混合?:就像你在上面的第一组代码中尝试过的那样。


Gordon L. Burditt


Hi, If i write:

#include <stdio.h>

int foo(int);

int main(void){
int a = 3;
foo(a);
}

int foo(int n){
n > 10 ? return 1 : return 0;
}

This code yields a compilation error, but if i write:

return n > 10 ? 1 : 0;

instead, it works. Why?
What does the standard say about that?

TIA

解决方案

"Nerox" <ne****@gmail.com> writes:

int foo(int n){
n > 10 ? return 1 : return 0;
}



`return'' is not an expression. It is a statement. You can''t use
it that way.
--
"It wouldn''t be a new C standard if it didn''t give a
new meaning to the word `static''."
--Peter Seebach on C99


Nerox wrote:

Hi, If i write:

#include <stdio.h>

int foo(int);

int main(void){
int a = 3;
foo(a);
}

int foo(int n){
n > 10 ? return 1 : return 0;
}

This code yields a compilation error, but if i write:

return n > 10 ? 1 : 0;

instead, it works. Why?
What does the standard say about that?



The ternary operator (<expr1> ? <expr2> : <expr3>) is an operator, not
a control structure. [1]

All expressions involved in the ternary operator must yield a value.
The statement "Return x" does not yield a value, in fact it returns
control to the caller function, so there is no one to return a value
to. That''s the reason why "n > 10 ? return 1 : return 0" gives
compilation errors.

return n > 10 ? 1 : 0, OTH is perfectly legal. The ternary operator
yields a value that is passed to return.

BTW, in your code it would be totally equivalent, and much cleaner to
simply write:

return n > 10;

[1] It can be used as a control structure. A sentence like:
n > 10 ? a = 1 : a = 0;
would be perfectly legal, but is bad practice. The reason why this is
legal is because an assigment does yield a value (the value being
assigned).


>int foo(int n){

n > 10 ? return 1 : return 0;
}

This code yields a compilation error, but if i write:

return n > 10 ? 1 : 0;

instead, it works. Why?
You cannot put *ANY* return statement in an expression.
You can, however, put an expression in a return statement.
What does the standard say about that?



A return statement is not an expression, therefore it can''t be
mixed with ?: like you tried to in the first set of code above.

Gordon L. Burditt


这篇关于在同一个表达式中返回两次语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆