(void)printf() vs. printf() 
Author Message
 (void)printf() vs. printf()

I've run up against something that I'm not too familiar with and was hoping
someone could explain.  I'll start by saying that I am still in the early
learning stages of C.

Anyways, I recently came across an exaample that used the following
structure:

(void)printf("Hello\n");

Whereas I'm more used to the following:

printf("Hello\n");

What's the purpose of the (void)?  It seemed to work either way, so I
really couldn't tell.

- Matthew
--
---
|  Matthew R. Zeier                       "Do what you can with           |

|   http://www.*-*-*.com/ ;                              |



Sat, 14 Dec 1996 12:40:41 GMT  
 (void)printf() vs. printf()


Quote:
> (void)printf("Hello\n");
> printf("Hello\n");

> What's the purpose of the (void)?  It seemed to work either way, so I
> really couldn't tell.

It is an explicit way of saying that the "return value of printf is
always ignored" - one of the most usual lint warnings. I think that
the cast is not needed and makes the code look ugly, even though it
makes the purpose obvious.

printf returns an int but what does it tell you? No one is really
interested! Lint suggests that you should be.

Jari

--
WWW URL                         * http://www.hut.fi/~jkokko/
Home page contains:             * snooker rules in hypertext format
                                * links to SBO gopher archives



Sat, 14 Dec 1996 14:00:50 GMT  
 (void)printf() vs. printf()

...

Quote:
> (void)printf("Hello\n");

> Whereas I'm more used to the following:

> printf("Hello\n");

> What's the purpose of the (void)?  It seemed to work either way, so I
> really couldn't tell.

It does work either way.  (void) indicates more clearly to a human
reader that the result returned by printf() (some indication of whether
it succeeded) is not being used.  A tool such as lint will give a
warning if a result is discarded implicitly without saying (void).

I use (void) myself, but note also that some argue that it is better for
detecting errors if you do not use it.

    Tony Bass

--
# Tony Bass                                     Tel: +44 473 645305

# Martlesham Heath, Ipswich, Suffolk, IP5 7RE   DO NOT e-mail to From: line
#                                               Opinions are my own



Sat, 14 Dec 1996 23:37:56 GMT  
 (void)printf() vs. printf()

Quote:


>> (void)printf("Hello\n");

>> printf("Hello\n");

>> What's the purpose of the (void)?  It seemed to work either way, so I
>> really couldn't tell.

>It is an explicit way of saying that the "return value of printf is
>always ignored" - one of the most usual lint warnings. I think that
>the cast is not needed and makes the code look ugly, even though it
>makes the purpose obvious.

Not exactly. It says: "the value returned by this call to printf is
ignored". The cast is not required by the language. It is used by
pedantic programmers to emphasize their intention to ignore the
return value. lint (or, at least, the one I've tried) is not shut up
by the cast.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sat, 14 Dec 1996 19:16:28 GMT  
 (void)printf() vs. printf()

: : It does work either way.  (void) indicates more clearly to a human
: : reader that the result returned by printf() (some indication of whether
: : it succeeded) is not being used.  A tool such as lint will give a
: : warning if a result is discarded implicitly without saying (void).

: : I use (void) myself, but note also that some argue that it is better for
: : detecting errors if you do not use it.

: For printf() specifically, I would probably be inclined not to use
: (void), as leaving it off is *very* common.  However, if I wrote a
: function which returned a value and I wanted to ignore it in some
: particular instance, I would always use (void) there.

Using (void) is simply a safeguard.  The thing I like least about C is
that is will do cast automatically, which can cause errors in some
cases.  Using explicit casts is just safer.



Mon, 16 Dec 1996 06:53:36 GMT  
 (void)printf() vs. printf()

Quote:
>Using (void) is simply a safeguard.  The thing I like least about C is
>that is will do cast automatically, which can cause errors in some
>cases.  Using explicit casts is just safer.

A safeguard against what?

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Mon, 16 Dec 1996 15:59:47 GMT  
 (void)printf() vs. printf()

|>Using (void) is simply a safeguard.  The thing I like least about C is
|>that is will do cast automatically, which can cause errors in some
|>cases.  Using explicit casts is just safer.

|A safeguard against what?

I don't get it either (but maybe that's normal in my particular case ;-)
The language itself is defined to allow the value of any expression
to be discarded:

6.6.3 Expression and null statements (ANSI-C 3.6.3)

Syntax

expression-statment:
        expression[opt] ;

Semantics

The expression in an expression statement is evaluated as a void
expression for its side effects.

[ end quote ]

Like I said: I don't get it; why should I turn on my verbosity
mode and write something silly like:

        (void)printf("Hello world!\n");

just because I have to express my intentions of discarding a
return value, while at the same time, nobody complaints when
I simply write something simple like:

        x++;

instead of:

        (void)x++;

I am discarding the value of the expression here too;
but nobody seems to care here in this particular case ...

My opinion is: I get what I deserve; if I want to discard
a (return) value, I'm allowed to do so, by definition of
the language. BTW, I don't mind other people sticking void
casts in front of expression statements, but I don't think
it's protecting them from anything but lint to keep its
big mouth shut ...

kind regards,


----------------------------------------------------------------------------
Two things are omnipresent in the universe: hydrogen and my stupidity



Mon, 16 Dec 1996 18:12:43 GMT  
 (void)printf() vs. printf()


Quote:
>Using (void) is simply a safeguard.  The thing I like least about C is
>that is will do cast automatically, which can cause errors in some
>cases.  Using explicit casts is just safer.

Casts are generally there to get the compiler to do things it would otherwise
reject, or to suppress warnings. There are times when casts are necessary
(often in circumstances where the code is non-portable) but where casts aren't
necessary I can't think of any reason why their inclusion would make the
code safer (OK - maybe when calling a function which isn't properly
prototyped, but that might be considered essential). In this particular case
of casting the return value of a function to void it doesn't make the
code any safer at all. It is a useful hint to the reader that the
programmer make a deliberate decision to ignore a return value. But this
isn't of much interest with some standard functions like printf.

--
-----------------------------------------


-----------------------------------------



Mon, 16 Dec 1996 23:31:36 GMT  
 (void)printf() vs. printf()

Quote:



>>Using (void) is simply a safeguard.  The thing I like least about C is
>>that is will do cast automatically, which can cause errors in some
>>cases.  Using explicit casts is just safer.

>Casts are generally there to get the compiler to do things it would otherwise
>reject, or to suppress warnings. There are times when casts are necessary
>(often in circumstances where the code is non-portable) but where casts aren't
>necessary I can't think of any reason why their inclusion would make the
>code safer (OK - maybe when calling a function which isn't properly
>prototyped, but that might be considered essential). In this particular case
>of casting the return value of a function to void it doesn't make the
>code any safer at all. It is a useful hint to the reader that the
>programmer make a deliberate decision to ignore a return value. But this
>isn't of much interest with some standard functions like printf.

Doesn't (void) printf() tell the compiler not to allocate space for the
return value of the printf() function?  I think that it does and this would
save space on the program stack at runtime.

John



Tue, 17 Dec 1996 02:38:34 GMT  
 (void)printf() vs. printf()


Quote:

>Doesn't (void) printf() tell the compiler not to allocate space for the
>return value of the printf() function?  I think that it does and this would
>save space on the program stack at runtime.

I don't think so.  All compilers I have worked with return function
values in one (or more) machine registers.  Discarding the return value
is a no-op: you simply don't do anything with the register's value.
Maybe you are right for some implementations of structure-returning
functions that use the stack to do that.

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Tue, 17 Dec 1996 04:48:18 GMT  
 (void)printf() vs. printf()

Quote:



>>Doesn't (void) printf() tell the compiler not to allocate space for the
>>return value of the printf() function?  I think that it does and this would
>>save space on the program stack at runtime.

>I don't think so.  All compilers I have worked with return function
>values in one (or more) machine registers.  Discarding the return value
>is a no-op: you simply don't do anything with the register's value.
>Maybe you are right for some implementations of structure-returning
>functions that use the stack to do that.

        Miguel is right, I believe.

        I think the key here is that the implementation of printf()
        has already been compiled and is sitting in a library some-
        where, with the stackframe already set up (in a machine-
        specific way) to make space for and return a value.

        The caller does not control how that stackframe is built,
        I would think.  Comments?

---




Tue, 17 Dec 1996 05:17:14 GMT  
 (void)printf() vs. printf()

Quote:


>> (void)printf("Hello\n");

>> printf("Hello\n");

>> What's the purpose of the (void)?  It seemed to work either way, so I
>> really couldn't tell.

>It is an explicit way of saying that the "return value of printf is
>always ignored" - one of the most usual lint warnings. I think that

Some ANSI C compilers can also be made to complain about this with the
right command line options.

Quote:
>the cast is not needed and makes the code look ugly, even though it
>makes the purpose obvious.

I think it forms good habits by always explictly stating that you are
not interested in the function's return value.  Granted, for functions
like printf() hardly anyone is ever interested in the return value, so
its a bit of a nuisance.

   Dan
--
====================== message is author's opinion only ======================
J. Daniel Smith         Bristol Technology Inc., Ridgefield, Connecticut (USA)

                        FTP: ftp.bristol.com     WWW: http://www.bristol.com



Mon, 16 Dec 1996 06:52:33 GMT  
 (void)printf() vs. printf()


Quote:
>Doesn't (void) printf() tell the compiler not to allocate space for the
>return value of the printf() function?  I think that it does and this would
>save space on the program stack at runtime.

Huh?

Ok... assume a compiler that allocates storage on the stack, and
printf knew about it.  If for some reason, the compiler didn't bother
to allocate storage on the stack, printf would still use some of the
space on the stack to return, and overwrite whatever was using the
space before it.

Most compilers (not all) I've used return values in registers, so this
is a non-issue.
--



Tue, 17 Dec 1996 09:54:08 GMT  
 (void)printf() vs. printf()

Quote:

>    I think the key here is that the implementation of printf()
>    has already been compiled and is sitting in a library some-
>    where, with the stackframe already set up (in a machine-
>    specific way) to make space for and return a value.
>    The caller does not control how that stackframe is built,
>    I would think.  Comments?

Ok, just answer one question....

if the caller doesn't know how the stackframe is made and printf
returns the value in it's own stack frame, how does the caller
get to it, especially since the stack frame would be deleted after
the function returns, and async signals could legitimately trash
whatever values have already been popped off the stack.

If the values get returned on the stack, it would definately have to
be the caller's responsibility to create a spot on the stack for
printf to return it's value, and it would always have to create it,
since printf would not be able to tell if had legitimate access to the
space or not.

(gee.. I wonder why most compiler writers return values in registers... :-)
--



Tue, 17 Dec 1996 10:53:26 GMT  
 (void)printf() vs. printf()

Quote:

>>the cast is not needed and makes the code look ugly, even though it
>>makes the purpose obvious.
>I think it forms good habits by always explictly stating that you are
>not interested in the function's return value.  Granted, for functions

        personally, I find the cast pretty distasteful, and
        not very useful:  simply the fact that a function's
        return value is neither stored nor referenced is a
        quite clear sign that it's not being used.

        The other commonly given reason is to make lint
        and/or the compiler shut up.  This strikes me as a
        ridiculous thing to do:  clearly written code should
        not be rewritten/obfuscated to suppress a diagnostic.
        Warnings are just that:  warnings.  I take them
        seriously and investigate the reason for the message,
        but if I don't see a problem with the code, I don't
        make any changes.  The stupidest change I've ever seen
        tho isn't the (void) printf() type of "casting", but
        the use of nonportable #pragma's to turn off warnings
        about used arguments.

        I wish I kknew from whence the "it compies with no
        warnings!" ego trip came.  "Uglified code" with no
        warnings does not mean better code.

    but, hey, this is a style/religious issue.  So whatever
    works for you...

--
Maurice S. Barnum               ==    I speak for me, not my employer.





Tue, 17 Dec 1996 11:09:27 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

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

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

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

4. newbie and (void)printf..

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

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

7. cout vs printf()

8. printf vs fprintf

9. printf %p vs. %x

10. 2 printf's vs. a ternary

11. cout vs printf

12. printf vs. vprintf

 

 
Powered by phpBB® Forum Software