Abdul Kotwala
Abdul Kotwala

Reputation: 21

Ambiguity of return values

In the C language book (by K&R), in the section on low level I/O , I came across two functions read() and close() both of which have an integer return type. But I have seen that they are being used without even caring to assign the return value to any integer variable. But when I create a user defined function having integer return type and use it without assigning it to integer variable it causes compiler warning. Why this inconsistency?

Upvotes: 0

Views: 86

Answers (1)

Lundin
Lundin

Reputation: 213513

Compilers traditionally don't warn for omitting the result of library function calls. Functions like printf, scanf and memcpy do return something, yet someone back in the dark ages of K&R decided to implicitly skip checking the result of the functions. It became de facto standard. Although to this day, skipping the result remains bad practice in many cases (like in the case of scanf).

Compilers do warn if you don't check the result of application functions though, because that's almost always a bug. If you deliberately don't want to check the result, you should write (void) func(); to silence such warnings.


(Side note: read and close aren't standard C, but Unix API. Still they are library functions.)

Upvotes: 1

Related Questions