newbie and (void)printf.. 
Author Message
 newbie and (void)printf..

Can someone explain why a call to a function would use (void) in
(void)printf for example. Can't the return value just be ignored?

Probably a stupid question....

Dave



Thu, 15 Jul 2004 13:07:02 GMT  
 newbie and (void)printf..

Quote:

> Can someone explain why a call to a function would use (void) in
> (void)printf for example. Can't the return value just be ignored?

It can be ignored just fine without the cast to void.  Some
people use such casts to explicitly indicate that the function's
return value is discarded.  This can also be used to placate
fussy lints or high compiler warning levels.


Thu, 15 Jul 2004 13:08:18 GMT  
 newbie and (void)printf..

Quote:

> Can someone explain why a call to a function would use (void) in
> (void)printf for example. Can't the return value just be ignored?

> Probably a stupid question....

Nope.  I've used "lint" tools - special programs that examine your source
code and check for a much greater number of potential problems than your
compiler is likely to - which complained bitterly about such trivial
things as "return value ignored" whenever calling printf.

Thing is, those programs are (sorta) right; if we'd checked the return on
printf, we could have noted that, for whatever reason, standard output
was now failing, and we could switch to writing log entries or some such.

However, I've seen damned little production code that actually goes to
this extreme; in most cases, the assumption is that if you can print, you
can print, so the reporting of this as a warning is annoying.

However, if you use (void)printf(...) then the analyzer knows you've
specifically and intentionally discarded the value - it may still think
it's a bad idea, but _you_ decided _specifically_ to do this, rather than
simply forgetting to test the value - so the program doesn't issue a
warning.

I presume there are also compilers out there which might be sufficiently
pedantic to consider this a case requiring a diagnostic, at least for
some warning level - just what you need, 49,000 warnings on something you
don't care about, effectively burying the messages you _do_ care about.



Sat, 17 Jul 2004 08:40:01 GMT  
 newbie and (void)printf..

Quote:


> > Can someone explain why a call to a function would use (void) in
> > (void)printf for example. Can't the return value just be ignored?

> > Probably a stupid question....

> Nope.  I've used "lint" tools - special programs that examine your source
> code and check for a much greater number of potential problems than your
> compiler is likely to - which complained bitterly about such trivial
> things as "return value ignored" whenever calling printf.

> Thing is, those programs are (sorta) right; if we'd checked the return on
> printf, we could have noted that, for whatever reason, standard output
> was now failing, and we could switch to writing log entries or some such.

> However, I've seen damned little production code that actually goes to
> this extreme; in most cases, the assumption is that if you can print, you
> can print, so the reporting of this as a warning is annoying.

> However, if you use (void)printf(...) then the analyzer knows you've
> specifically and intentionally discarded the value - it may still think
> it's a bad idea, but _you_ decided _specifically_ to do this, rather than
> simply forgetting to test the value - so the program doesn't issue a
> warning.

> I presume there are also compilers out there which might be sufficiently
> pedantic to consider this a case requiring a diagnostic, at least for
> some warning level - just what you need, 49,000 warnings on something you
> don't care about, effectively burying the messages you _do_ care about.

I have been known to write:

#define Printf (void)printf

for just that reason.  However PCLINT at least has a shutoff for
the generic printf function class.  (I know, most here gaze at the
define in horror)

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Sat, 17 Jul 2004 15:11:40 GMT  
 newbie and (void)printf..
[snips]

Quote:

> I have been known to write:

> #define Printf (void)printf

> for just that reason.  However PCLINT at least has a shutoff for the
> generic printf function class.  (I know, most here gaze at the define in
> horror)

Well, maybe a little, but it could be worse.  Did you ever, way back
when, catch yourself "Pascalizing" C? :)


Fri, 23 Jul 2004 21:30:01 GMT  
 newbie and (void)printf..

Quote:

> [snips]


> > I have been known to write:

> > #define Printf (void)printf

> > for just that reason.  However PCLINT at least has a shutoff for the
> > generic printf function class.  (I know, most here gaze at the define in
> > horror)

> Well, maybe a little, but it could be worse.  Did you ever, way back
> when, catch yourself "Pascalizing" C? :)

Yup.  I don't "#define BEGIN {" if that's what you mean, but I do
use the sort of structured code organization that Pascal insists
on.  By which I mean:

T f(blah foo)
{
...

Quote:
}

t2 f2(blah2 bar)
{
   ...
   x = f(bar);
Quote:
}

...
int main(void)
{
   ...
   y = f2(foobar);
   ...

Quote:
}

where everything is defined before use with minimal prototypes.  I
also use typedef as a poor mans substitute for type.  Can't find a
*suitable* substitute for sub-ranges, run-time checks and sets
though.  And "#include <iso646>" is useful.

IMO C before proper function headers (i.e. parameter type
checking) was not a suitable language for anything.  The fact that
people made many things work is nearly miraculous.  I think the
majority of silly foulups are due to unchecked parameters in
variadic functions, which need to be avoided.

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)



Sat, 24 Jul 2004 04:35:20 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. (void)printf() vs. printf()

2. Re(2): Re(2): (void)printf() vs. printf()

3. Re(2): (void)printf() vs. printf()

4. from void (void*) to void (_cdecl*) (void*)

5. Speed consideration in (void)printf(..)

6. printf("%p\n",(void *) ptr);

7. what's the difference between void main(void) and int main(void)

8. difference between void foo(void) and void foo()

9. replacing printf with my printf, then calling C lib's printf

10. Newbie Help With void*

11. newbie void ptr problem - I think

12. Newbie: void pointers to functions

 

 
Powered by phpBB® Forum Software