c语言中这种编码风格是什么样的?
(void) fputs( line, stdout );
(void) alarm( TIMEOUT );
上面出现在函数体中,我以前从未见过这样的代码......
(void) fputs( line, stdout );
(void) alarm( TIMEOUT );
The above appears in the function body,I've never seen such code before...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种为调试工具编写代码的形式,以人类阅读代码为代价,并且它被认为是有害的。特别是,如果您没有使用返回值的函数的返回值,经典的 lint 工具和某些编译器可能会生成警告,而在本地抑制这种情况的唯一方法是将结果转换为
无效
。如果您有足够的权限,可以忽略它,也可以修复它(删除无用的
void
强制转换)。It's a form of coding to debugging tools at the expense of human beings reading your code, and it's Considered Harmful. In particular, the classic
lint
tool and perhaps some compilers generated warnings if you did not use the return value of a function that returned a value, and the only way to suppress this locally was to cast the result tovoid
.Either ignore it, or fix it (remove the useless
void
casts) if you have sufficient authority to do so.唯一不寻常的是强制转换为
void
。这是一种指示您将忽略函数的返回值,或指示函数不返回值的方法。一些编译器会警告忽略的返回值,因此可以绕过该警告。我自己认为这不是很好的风格。The only thing unusual are the casts to
void
. It's a way to indicate that you're going to ignore the return value of the function, or to indicate that a function returns no value. Some compilers warn about ignored return values, so can be a way around that warning. I don't think it's very good style myself.如果您指的是
(void)
部分,那么这就是显式丢弃函数调用的结果。fputs(...)
返回一个int
。(void) fputs(...)
丢弃int
返回值而不生成编译器警告。If you mean the
(void)
part, then that is explicitly discarding the result of the function call.fputs(...)
returns anint
.(void) fputs(...)
discards theint
return value without generating compiler warnings.