5 ms·
> Also, casting function calls to (void) is nonsensical. Does extremely pedantic C require the results of function calls to be used? I know the correct way to
by wyager 13y ago
> Also, casting function calls to (void) is nonsensical.
Does extremely pedantic C require the results of function calls to be used?
I know the correct way to mark a variable as unused is to cast it to void, but I'm not sure if you're supposed to do that for function return values as well.
- theseoafs 13y agoNothing about the cast is "required". However, it's good practice because the cast explicitly acknowledges the function has a return value that we're throwing out. Looking at the following function call: (void)create_widget(&widget); It's clear that `create_widget` returns some value -- it's probably an error code that tells us whether the widget creation was successful. The source code here says "I know this function returns an important value, but I'm going to disregard it here". This could be useful for debugging if you find that, for example, an error condition is being handled improperly (e.g. "oh, we're obviously throwing out the return value of this function, which we shouldn't be doing"). This function call is much less informative: create_widget(&widget);
- tptacek 13y agoIt's a terrible and (I think) amateurish practice that misapprehends the point of C's type checking while adding lots of visual noise, which is why you'll virtually never see it in well-liked C code.