Search code examples
cvoid

What is (void) with () in C


I recently found this line of code, but I don't know what the void with () mean. Can someone help me ? Thanks

(void) myFunc();

Solution

  • (void) has the form of a cast operation, but casting to void (note: not to void *) is normally not a useful thing to do.

    In this context, though, (void) myFunc(); means that myFunc returns a value, and whoever wrote this line of code wanted to throw that value away and did not want the compiler to complain about this, and/or wanted to make it clear to future readers of the code that they were throwing the value away on purpose. In the generated code, (void) myFunc(); has exactly the same effect as myFunc(); with nothing in front.

    Due to historic abuses of this notation, some compilers will warn you about not using the value of certain functions (e.g. malloc, read, write) even if you put (void) in front of them, so it's less useful than it used to be.