mcp_achindra said:
(void) printf(...) Vs printf(...)
Well I think I figured out, what it all means. We have been running
all the way on the wrong tracks.
The ANSI/ISO C Standards Documentation - States
"Portable Codes - Strictly Conforming"
- only uses specified features
- doesn't exceed any implementation-defined limits
- has no output that depends on implementation -defined, unspecified
or undefined limits.
This intends to describe maximum portable programs, which will always
produce same output, what ever they are run on.
To make a program portable, one must put the necessary casts, return
values adn so on.
Thus it is now clear thet (void) printf(...) Vs printf(...) is merely
an implementation issue and this makes the code portable over various
platforms.
It's not about portability, but about documentation.
C has various kinds of statements: return, if, { blocks },
and others. One frequently-used type of executable statement
consists only of an expression followed by a semicolon, as in
x = 3; /* the `=' operator and two operands */
i++; /* the suffix `++' operator and one operand */
f(); /* the `()' operator and one operand */
The interesting thing about an expression is that it yields
a value. Yes, even the `=' operator produces a value, in addition
to the effect of storing something: in `x = 3;' the value of the
entire expression is three, converted to the type of `x'.
Sometimes you don't care about the value of an expression;
you evaluate it only for its side-effects. You probably write
`x = 3;' just to store a new value in `x', making no use of the
expression's value. You probably write `i++;' just to increment
`i', and you don't use the previous value (which is the value
of the expression) at all. You probably write `f();' just to
accomplish whatever side-effects the function produces, not
caring about the value it returns.
... and that's where the `(void)' casts come in. The printf()
function returns a value: either the number of characters that
were printed, or a negative value to indicate an output error.
You frequently don't care how many characters were printed, and
you may not feel like checking for errors (either through general
sloppiness or because you plan to check with ferror(stdout) later
on). Yet, the function actually does return a value -- and some
source-checking tools like lint will issue all kinds of warnings
about ignoring that value. This can have the unpleasant effect of
producing so many warnings about printf() calls that the important
messages just get lost in the flood.
So you write `(void)printf(...);' as a way of "using" the
returned value: you "use" it by throwing it away, which has the
effect of ignoring it but is more explicit. "Quiet, lint," you
are saying, "I know that I'm ignoring the value, and it's not
an oversight you should complain about." The program does the
same thing with or without the `(void)' cast, so there's no
issue about portability; the cast amounts to documentation that
tells lint -- or a human reader -- that you really intended to
ignore the returned value.
Personally, I think such casts are just unpleasant clutter:
the way to silence lint-like tools is to make the tools smarter,
not to cater to their nervousness. But some coding standards
require such things (and the position is not unreasonable; maybe
one *should* pay attention to the possibility that the output
never went anywhere), so I can't really make a cast that the
`(void)' cast is a Bad Thing. You pays your money, and you takes
your choice.