Possible Duplicate:
casting unused return values to void
What is the purpose of (void)
before a function call, for example
(void)func1();
I assume this is the same as simply calling func1();
Therefore is the (void)
call simply to let other programmers know that the return type will be ignored, for instance if func1()
had a return type of int
, or does the compiler perhaps perform some optimizations on the function? Perhaps there is another reason behind it altogether - is it even legal C++ or perhaps is it a remnant of C seen in some legacy code.
Thanks
In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal. When used in a function's parameter list, void indicates that the function takes no parameters.
It means very little. It is explicitly turning the line to a void expression, that is an expression that is only used for its side effects and whose value is discarded. So the lines func1(); and. (void)func1();
If you omit the void , you have to search for function() and will therefore also find all function calls, making it more difficult to find the actual implementation. Show activity on this post. In C++, there is no difference in main() and main(void) . But in C, main() will be called with any number of parameters.
It prevents warning if some function are declared with attribute : "Warn if return value not used/checked"
Maybe a duplicate of Warning: ignoring return value of 'scanf', declared with attribute warn_unused_result
Check the documentation of gcc : http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html (warn_unused_result) for more details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With