3

I was going through some code and came across a piece of code which was like this:-

void func(int a)
{
   int temp;
       ...
       ....
   temp=a;
   (void)temp;
}

What does the temp variable do here? If I compile it using -Wall option, I do not get any compilation error or warning either. Please help me understand this code style. Thanks.

ShreshthaB
  • 31
  • 3
  • 1
    In the actual snippet you post, the `(void)temp` looks very odd indeed. – NPE Aug 31 '13 at 18:38
  • What NPE said. This is a situation where you really do want the compiler to warn you about that, so you can remove the unnecessary local. – T.J. Crowder Aug 31 '13 at 18:38

3 Answers3

6

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.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • Thanks a lot for the explanation. – ShreshthaB Aug 31 '13 at 20:09
  • I tried gcc -O3 -g -Wall -Wextra -c void.c on my Ubuntu machine which uses gcc version (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 but I did not get any error or warning. Yes, you are correct. temp collects the return value of a system call namely write(). But the func() itself does not have a conditional compilation within it. Thanks for the pointer though.I will keep that in mind while browsing the code. – ShreshthaB Aug 31 '13 at 20:18
  • The 'set but unused' warning was added in GCC 4.6.0; it was not in GCC 4.5.x. I was able to test 4.5.1, 4.5.2, 4.6.0, 4.7.0, 4.7.1, 4.8.1 (I keep copies of them — but the 4.4.2 instance is bust, missing a library). – Jonathan Leffler Aug 31 '13 at 20:24
0

Cheat the compiler: Pretend as temp was used, but in fact do nothing.

alk
  • 66,653
  • 10
  • 83
  • 219
0

It evaluates the expression temp (a simple but still a valid expression) and tells the compiler that the result of the expression is discarded by casting the result of the expression to void (which means "no type").

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550