return 是运算符还是函数?
我认为这太基础了,但这两者是如何工作的呢?
return true; // 1
和
return (true); // 2
类似的:sizeof
,exit
我的猜测:
如果 return 是一个函数,则
1
将是 错误。所以,返回应该是一个
一元 运算符
也可以接受 括号...很像一元 减号
:-5
和-(5)
,两者都是 好的。
这就是一元运算符吗?
This is too basic I think, but how do both of these work?
return true; // 1
and
return (true); // 2
Similar: sizeof
, exit
My guess:
If return was a function,
1
would be
erroneous.So, return should be a
unary
that can also take in
operator
brackets... pretty much likeunary
:
minus-5
and-(5)
, both are
okay.
Is that what it is - a unary operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
return
是关键字,操纵控制流。它类似于if
、for
等。它可以与或不与表达式一起使用(return;
从void 返回
函数)。当然,与所有表达式一样,允许使用额外的括号。 (因此return (42);
类似于int i = (4*10+2);
,在这两种情况下,括号都是多余的,但也是允许的。)sizeof
是一个关键字,它是一个运算符,类似于new
、delete
、+
、->
、::
等std::exit()
是一个标识符,表示 C 标准库的函数(永远不会返回给调用者)。return
is a keyword that manipulates control flow. In that it's similar toif
,for
etc. It can be used with or without an expression (return;
returns from avoid
function). Of course, as with all expressions, extra parentheses are allowed. (Soreturn (42);
is similar toint i = (4*10+2);
, in both cases the parentheses are redundant, but allowed.)sizeof
is a keyword that is an operator, similar tonew
,delete
,+
,->
,::
, etc.std::exit()
is an identifier that denotes a function of the C standard library (which never returns to the caller).return
只是一种语言/控制流构造。它当然不是一个函数,因为它在语法上是不可约的,而且它也不是真正的运算符,因为它没有返回值。return
is just a language/control flow construct. It's certainly not a function, since it's syntactically irreducible, and it's not really an operator either, since it has no return value.return
不是运算符,也不是函数。return
是构成return 语句的关键字,属于跳转语句类别。在这方面,它与sizeof
或exit
完全没有相似之处。在 C 的古代预标准版本(例如 CRM C)中存在将
()
放在return
参数周围的要求,但很快就被消除了,尽管即使在今天,将return
参数包装在多余的()
中的奇怪习惯仍然时常可见。return
is not an operator and is not a function.return
is a keyword that forms a return statement, which belongs to the category of jump statements. In that regard it has absolutely no similarities with eithersizeof
orexit
.The requirement to put
()
around the argument ofreturn
existed in ancient pre-standard versions of C (CRM C, for example), but was quickly eliminated, even though the quirky habit to wrap the argument ofreturn
in superfluous()
can be seen from time to time even today.return 是一个控制流关键字,就像
goto
、break
、continue
、if
、else< /code> ...不要将其视为运算符,因为它不会改变其背后的值。
()
只是计算表达式,计算表达式的结果将传递给调用函数(如何取决于编译器实现)。它当然也不是函数,只要想一想:你会如何从 return 返回?
return is a control flow keyword, just like
goto
,break
,continue
,if
,else
... Do not think of it as an operator, because it does not alter the value behind it. The()
are just to evaluate expressions and the result of the evaluated expression will be passed along to the calling function (how depends om compiler implementation).It is also certainly no function, just think about it: how would you return from return?
“return”既不是例程,也不是运算符。
它转换为众所周知的汇编指令。例如,在 x86 架构上,它翻译为“ret”,在 PowerPC 架构上,它翻译为“blr”。
对于它返回的值,编译器在发出返回指令之前将该值移动到适当的寄存器中。在 x86 架构上,如果需要的话,这通常是 EAX 和 EDX ——对于 x86-64,寄存器会略有变化。在 PPC 上,如果没记错的话,它是 r1——如果我在这个细节上错了,其他人可能会纠正我。
希望这有帮助。
"return" is neither a routine nor an operator.
It translates to well known assembler instruction. For example, on the x86 architecture, it translates to "ret", and on the PowerPC, architecture it translates to "blr".
For the value it returns, the compiler moves that value into the appropriate register(s) prior to issuing the return instruction. On the x86 architecture, this is typically EAX and EDX if necessary--the registers will change slightly for x86-64. On PPC, if memory serves, it is r1--others may correct me if I am wrong on that detail.
Hope this helps.
return 后面始终可以跟一个表达式,但对于返回类型检查,表达式必须与函数的返回类型具有相同的类型。
因此,您可以这样概括它
:(在具有
void
返回类型的函数中,return
后面可能不跟有表达式;裸露的return
只需退出该函数即可。)return can always be followed by an expression, but for the return to type check, the expression must have the same type of the return type of the function.
hense you can generalize it by saying
(In a function with a
void
return type,return
may not be followed by an expression; a barereturn
simply exits the function.)“将 () 放在 return 参数周围的要求存在于 C 的古代预标准版本(例如 CRM C)中,但很快就被消除了,尽管将 return 参数包装在多余的 () 中的奇怪习惯可以即使在今天,也会时不时地看到。”
是的,你知道你正在查看一些旧代码,或者当你看到有人一直使用括号时,他们认为 return 是一个函数。
我的大学老师就这么做了,这一直让我很恼火。
哦,好吧,至少他是一致的。
"The requirement to put () around the argument of return existed in ancient pre-standard versions of C (CRM C, for example), but was quickly eliminated, even though the quirky habit to wrap the argument of return in superfluous () can be seen from time to time even today."
Yeah, you know you are looking at some old code or someone thinks return is a function when you see them using parens with it all the time.
My college instructor did that and it annoyed me all the time.
Oh well at least he was consistent.