C 中围绕返回值的括号 [英] Parenthesis surrounding return values in C

查看:30
本文介绍了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 中围绕返回值的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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