1

Some questions tackle with this problem for general instances; however, in this case, all I see is just a value returned by a function. See by yourself:

The third line, (void) signal (SIGALRM, myAlarm); is supposed to execute the system call signal, which - if I understood correctly - returns a pointer to a function.

Being it the general case, I can see why a function pointer is being cast to void; however, apart from C's implicit casting, that line itself does exactly nothing to the returned value "in memory" as it's simply a value.

Am I missing something? Does (void) actually do anything in the third line (and for the rest of the program)? Even if the function pointer were to be of a different type than void, wouldn't the third line act only on the returned value, instead of a (hypothetically) stored one? In a few words, is it necessary or is it a redundancy?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • About the similar questions: I discarded them because they are based on C++, not C. In this case, I am referring to C code. However, combining the answer in this question and the ones in the similar questions, I guess that this is true for C as well. Apparently, void casting is a special type of casting, with respect to any other action. – Maurizio Carcassona Feb 11 '21 at 06:37

1 Answers1

2

This is to suppress the compiler warning about an unused return value (from the function call).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • So it's safe to say that 1) in this case we have no implicit casting because there is no lvalue; 2) the cast is used as a form of "use" of the value, hence it would be equivalent to use `signal (SIGALRM, myAlarm)==0`, just for the sake of "using" the returned value? If that's the case, I really should start worrying about obvious warnings, as I honestly don't care about them when using gcc, – Maurizio Carcassona Feb 11 '21 at 06:26
  • 1
    @MaurizioCarcassona `I really should start worrying about obvious warnings,` you MUST. – Sourav Ghosh Feb 11 '21 at 07:13
  • You're right, especially in C thank you for the answer by the way – Maurizio Carcassona Feb 11 '21 at 07:42
  • Aside: It's not entirely clear in the standard how a function pointer or any other type is explicitly converted to `void`. Section 6.3.2.2 does not describe how non-void expressions are converted to `void`. It only describes the effect of an expression _evaluated as_ a void expression. (An example of something _evaluated as_ a void expression is the left operand of the comma operator.) It leaves it to the reader to guess that converting the value of an expression to `void` has the same effect as evaluating the expression as a void expression. – Ian Abbott Feb 11 '21 at 12:08
  • However, the standard does have an example of a function return value being explicitly converted to `void` by a cast - example 1 of section 6.8.3. – Ian Abbott Feb 11 '21 at 12:22