23

Many times I see in open source code that a call to a C function is cast to void.

For example, in the source code for ls (http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/ls/ls.c) I see the following.

(void) setlocale(LC_ALL, ""); 

Why is this good practice?

2

1 Answer 1

33

It explicitly means you ignore the return value, and did not just forget it.

Sign up to request clarification or add additional context in comments.

4 Comments

Some static analyzers warn if a return value is silently ignored and don't warn when you explicitly ignore by casting to void. It was more important when we had only error codes and no exceptions ...
@Peter G.: This question is tagged C. They still don't have exceptions.
@David good point! :) Then let's forget about the second sentence of my first commend.
Its a way to hide the fact for the static analyzer that you are ignoring that error code that you should have checked; and that in three years some poor slub will have to debug a problem caused by the fact that you did not check the error code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.