puf*_*der 5 c coding-style function void
我最近遇到了一个相当不寻常的编码约定,其中对返回"void"的函数的调用以(void)为前缀.
例如
(void) MyFunction();
Run Code Online (Sandbox Code Playgroud)
它与函数调用有什么不同:
MyFunction();
Run Code Online (Sandbox Code Playgroud)
它有没有任何优势,或者它是另一个不必要但有某种编码约定?
小智 12
某些函数(如printf())返回的值几乎从未在实际代码中使用(在printf的情况下,打印的字符数).但是,有些工具,比如lint,期望如果函数返回一个必须使用的值,并且除非你写下类似的内容,否则会抱怨:
int n = printf( "hello" );
Run Code Online (Sandbox Code Playgroud)
使用void cast:
(void) printf( "hello" );
Run Code Online (Sandbox Code Playgroud)
是一种告诉这些工具的方法,你真的不想使用返回值,从而保持它们的安静.如果您不使用此类工具,则无需费心,在任何情况下,大多数工具都允许您将它们配置为忽略特定函数的返回值.