0

inotify file in C

I have seen the following codes used to call

(void) inotify_rm_watch(fd, wd);
(void) close(fd);

Why not?

inotify_rm_watch(fd, wd);
close(fd);

What is the difference between the two usages?

Community
  • 1
  • 1
q0987
  • 34,938
  • 69
  • 242
  • 387
  • Do you want a C++ answer to a C question? – NathanOliver May 16 '16 at 13:53
  • 1
    Possible duplicate of [void cast of argc and argv](http://stackoverflow.com/questions/8052091/void-cast-of-argc-and-argv) – Ulrich Eckhardt May 16 '16 at 13:59
  • 1
    The programmer wants to document that they are stupid (knowingly ignoring returnvalues and not checking for errors) as opposed to just being careless. That said, the explicit cast to void has been discussed here before. – Ulrich Eckhardt May 16 '16 at 14:01

2 Answers2

6

There are some cases when ignoring return value of the function causes compiler to warn you about it.

Casting return type to void effectively suppresses the warning. However, the wisdom of ignoring return type is questionable. If function return something, you probably want to know what it returned? Just in case there was a problem, you know.

In particular, inotify_rm_watch returns -1 when the function fails - and you are usually interested to know it. On the other hand, checking for return value of close is usually not neccessary and borders with paranoia :)

SergeyA
  • 61,605
  • 5
  • 78
  • 137
-3

There is no difference.

The semicolon (;) effectively means "discard the result you have". Casting to void does nothing in this context.

Shachar Shemesh
  • 8,193
  • 6
  • 25
  • 57