本文介绍了在C中用括号括起来的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ANSI C代码中,经常可以看到括号将单个返回值包围起来。

Quite often in ANSI C code I can see parenthesis sorrounding a single return value.

就像这样:-

int foo(int x) {
  if (x)
    return (-1);
  else
    return (0);
}

在这种情况下,为什么在返回值附近使用()?有任何想法吗?我看不出有任何理由。

Why use () around the return value in those cases? Any ideas? I can see no reason for that.

推荐答案

确实没有理由……这只是旧的惯例。

There really isn't a reason...it's just old convention.

为了节省空间,程序员通常会在返回行中而不是在返回行中做最后的数学运算,并且确保大多数都在括号内,以便于查看返回的单个语句,例如:

To save space, programmers would often do the final math in the return line instead of on it's own line and the parens ensure are mostly there to make it easier to see that it is a single statement that is returned, like this:

return (x+i*2);

而不是

int y = x+i*2;
return y;

括号成为一种习惯,它被卡住了。

The parenthesis became a habit and it stuck.

这篇关于在C中用括号括起来的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 06:44