I came across some code that puts a (void) cast before each call to printf. Why?

Q

I came across some code that puts a (void) cast before each call to printf. Why?

✍: Guest

A

printf does return a value (the number of characters printed, or an error code), though few programs bother to check the return values from each call. Since some compilers (and lint) will warn about discarded return values, an explicit cast to (void) is a way of saying ``Yes, I've decided to ignore the return value from this call, but please continue to warn me about other (perhaps inadvertently) ignored return values.'' It's also common to use void casts on calls to strcpy and strcat, since the return value is never surprising.

2015-05-13, 971👍, 0💬