Need for prefixing a function with (void)

前端 未结 6 1876
一向
一向 2020-12-03 23:32

I recently came across a rather unusual coding convention wherein the call for a function returning \"void\" is prefixed with (void).

e.g.

(void)         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-03 23:45

    Some functions like printf() return a value that is almost never used in real code (in the case of printf, the number of characters printed). However, some tools, like lint, expect that if a function returns a value it must be used, and will complain unless you write something like:

    int n = printf( "hello" );
    

    using the void cast:

    (void) printf( "hello" );
    

    is a way of telling such tools you really don't want to use the return value, thus keeping them quiet. If you don't use such tools, you don't need to bother, and in any case most tools allow you to configure them to ignore return values from specific functions.

提交回复
热议问题