It means that the value of temp
is otherwise unused, and the optimizing compiler warns about it (variable is set but not used), but by including the (void)temp;
, you (currently) get away without the warning from the compiler. The line generates no code as well as no compiler warning.
Given the source code:
extern void func(int a);
void func(int a)
{
int temp;
temp = a;
//(void)temp;
}
GCC 4.8.1 on Mac OS X 10.8.4 gives the warning for the code:
$ gcc -O3 -g -std=c11 -Wall -Wextra -c void.c
void.c: In function ‘func’:
void.c:4:8: warning: variable ‘temp’ set but not used [-Wunused-but-set-variable]
int temp;
^
$
With the comment marker removed (so (void)temp;
is compiled), the compilation generates no warning.
This is clearly example code distilled from some larger context (an SSCCE (Short, Self-Contained, Correct Example) — thank you for producing one!). There may be a number of reasons (of greater or lesser validity) why the code appears. Often, it will be because there is conditionally compiled code after the assignment to temp
which will use the assigned value. Instead of being an assignment of a function parameter, it will often be the result of a function call:
void func(int a)
{
int temp;
...
temp = some_function(a);
#if defined(SOMETHING_OR_OTHER)
...do stuff with temp...
#else
(void)temp;
#endif
}
This notation keeps the single function call, but avoids complaints from the compiler when SOMETHING_OR_OTHER
is not defined.