Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of (void) before a function call [duplicate]

Tags:

c++

c

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

like image 922
const_ref Avatar asked Dec 19 '12 14:12

const_ref


People also ask

Why void is used before any function?

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.

What does void * Before function mean?

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();

Is void necessary in C?

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.


1 Answers

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

like image 154
benjarobin Avatar answered Oct 06 '22 04:10

benjarobin