(void) printf vs printf

W

whatluo

Hi, c.l.cs

I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout\n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.

Thanks,

Whatluo.
 
W

Walter Roberson

I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout\n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.

It can silence a compiler (or stand-alone "lint" program) warning
that the result of a function has been thrown away.

Also, if the programmer is being particularily careful to check
error conditions at every place errors can occur, then
a (void) on the printf() can be a signal that the lack of error
checking on that one statement is a deliberate design decision
instead of something that was overlooked.
 
C

Christian Bau

"whatluo said:
Hi, c.l.cs

I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout\n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.

printf returns a value.

When a function returns a value, and the programmer ignores the code, I
would want to know why. There are usually two explanations: The
programmer made a mistake ignoring the return value, or the programmer
did ignore the return value on purpose. When you write

printf (...);

I have no idea if you wanted to ignore the return value or not. If you
write

(void) printf (...);

it tells me that the programmer knew that printf returns a value, and
made a conscious decision to ignore that value. Do some professional
programming, try finding a few bugs in a million lines of other people's
code, and you will appreciate such things.
 
S

SM Ryan

# In article <[email protected]>,
#
# > Hi, c.l.cs
# >
# > I noticed that someone like add (void) before the printf call,
# > like: (void) printf("Timeout\n"); while the others does't. So
# > can someone tell me whether there any gains in adding
# > (void) to printf.
#
# printf returns a value.
#
# When a function returns a value, and the programmer ignores the code, I
# would want to know why. There are usually two explanations: The
# programmer made a mistake ignoring the return value, or the programmer
# did ignore the return value on purpose. When you write

I routinely notice lots of people checking the return of printf
instead of simply looking at the terminal window.
 
G

Giorgos Keramidas

SM Ryan said:
# In article <[email protected]>,
#
# > Hi, c.l.cs
# >
# > I noticed that someone like add (void) before the printf call,
# > like: (void) printf("Timeout\n"); while the others does't. So
# > can someone tell me whether there any gains in adding
# > (void) to printf.
#
# printf returns a value.
#
# When a function returns a value, and the programmer ignores the code, I
# would want to know why. There are usually two explanations: The
# programmer made a mistake ignoring the return value, or the programmer
# did ignore the return value on purpose. When you write

I routinely notice lots of people checking the return of printf
instead of simply looking at the terminal window.

Of course, sometimes just staring at terminals may be deceptive at
times. Consider a program that prints whitespace separated words and
shows two words on your terminal:

hello world

Have these been printed by a single call to printf?

printf("hello world\n");

Two successive calls?

printf("hello ");
printf("world\n");

Or maybe three calls, where the middle one failed, for some obscure
(and undetected by just staring at the output) reason?

printf("hello ");
printf("strange ");
printf("world\n");
 
R

Richard Bos

whatluo said:
I noticed that someone like add (void) before the printf call,
like: (void) printf("Timeout\n"); while the others does't. So
can someone tell me whether there any gains in adding
(void) to printf.

Nothing. It shuts up geriatric versions of lint, that's all. But you're
better off using a lint that wasn't designed on clay tablets anyway.

Richard
 
R

Richard Bos

P.J. Plauger said:
Note, however, that these rules do *not* apply to casting
the value returned by malloc. So, despite the fact(s) that

-- a (void) cast is *never* needed in a conforming C program, and

-- it's only real-world effect is to silence diagnostics in
a non-standard dialect of C (lint)

it is nevertheless a Good Thing (TM) to write (void) casts and
a Bad Thing (SM) to do exactly the same thing for the return
value of malloc.

No, because of those reasons, and more, it is a Bad Thing to write
(void) casts and also a Bad Thing to cast malloc().
Go figure.

Furrfu.

Richard
 
B

Bhatnagar Achindra

int printf(...) and void printf(...)

When we use printf(...) and we don't handle the return values the
return value is destroyed with the loading and execution of the next
statement call.

When use void printf(...) we instruct the system to flush out any
return values before moving to the next statement, thus we clean up any
possibility of fetching the return value.

Positive values from the function call specifies the number of bytes
written to STDOUT, excluding '\0' in general, while a negative value
tells the error code if occured.
 
N

No Such Luck

Also, if the programmer is being particularily careful to check
error conditions at every place errors can occur, then
a (void) on the printf() can be a signal that the lack of error
checking on that one statement is a deliberate design decision
instead of something that was overlooked.

Interesting. I knew things like successful file openings should be
error checked, and pointers should be checked before accessing, etc.
But printfs? What is usually the accepted degree of error checking?
 
E

Eric Sosman

No said:
Interesting. I knew things like successful file openings should be
error checked, and pointers should be checked before accessing, etc.
But printfs? What is usually the accepted degree of error checking?

How important is the output? What are the consequences
if it fails to appear?

<whimsy>

A complete absence of error checking suggests that the
programmer places no value on the output and doesn't care
whether it appears or not. If so, the program's efficiency
can probably be improved by removing all the printf() calls,
along with the computations that produce data for them.
Following this principle, here is an optimized version of
the canonical "Hello, world!" program:

int main(void) {
return 0;
}

A great many programs can be optimized this way, and it
turns out that quite a few of them, after optimization,
become identical to the code shown above. This is a great
savings, because debugging and testing this one piece of
code essentially debugs and tests all those other programs
at the same time. A triumph for code re-use!

</whimsy>
 
J

Jens.Toerring

Bhatnagar Achindra said:
int printf(...) and void printf(...)
When we use printf(...) and we don't handle the return values the
return value is destroyed with the loading and execution of the next
statement call.
When use void printf(...) we instruct the system to flush out any

I guess you meant "( void ) printf( .... )", didn't you?
return values before moving to the next statement, thus we clean up any
possibility of fetching the return value.

No, cast to void or not doesn't "instruct the system" to do anything.
It only explicitely tells the compiler (or other programs or perhaps
other programmers that analyze the surce code) that the return value
of printf() will never get used. The compiler can find out about this
all by itself and will create identical code with and without the cast.
Positive values from the function call specifies the number of bytes
written to STDOUT, excluding '\0' in general,

printf() never writes out a '\0', so it can't exclude it from the count.
Only function printing into strings like sprintf() would write out a
'\0' and for these function the final '\0' never gets included into the
count.
while a negative value tells the error code if occured.

A negative return value just indicates that there was an error,
it's not an error code in the sense that you could derive from
the value what went wrong.
Regards, Jens
 
C

CBFalconer

.... snip ...


printf() never writes out a '\0', so it can't exclude it from the
count. Only function printing into strings like sprintf() would
write out a '\0' and for these function the final '\0' never gets
included into the count.

<nitpick> Counter example: (1 == printf("%c", '\0')); </nitpick>

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
J

Jens.Toerring

<nitpick> Counter example: (1 == printf("%c", '\0')); </nitpick>

Right you are - I was only thinking about the one at the end of
a string written by sprintf() etc.

Regards, Jens
 
K

Keith Thompson

No Such Luck said:
Interesting. I knew things like successful file openings should be
error checked, and pointers should be checked before accessing, etc.
But printfs? What is usually the accepted degree of error checking?

It's largely a question of how you'd handle the error. In the
simplest case, the classic "hello, world" program:

#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}

you could do something like this:

#include <stdio.h>
int main(void)
{
int result = printf("Hello, world\n");
if (result < 0) {
/* handle the error? */
}
return 0;
}

A failure in printf() generally indicates that the program is unable
to produce output. What are you going to do, print an error message?

Conceivably you could print an error message to stderr (which *might*
not be having the same problems that stdout is having), or abort the
program with an error status, but most programmers don't bother most
of the time.

If C had a decent exception handling mechanism, and the standard
library routines used it, it would provide a good way to handle this
kind of error.
 
S

SM Ryan

# ># In article <[email protected]>,
# >#
# ># > Hi, c.l.cs
# ># >
# ># > I noticed that someone like add (void) before the printf call,
# ># > like: (void) printf("Timeout\n"); while the others does't. So
# ># > can someone tell me whether there any gains in adding
# ># > (void) to printf.
# >#
# ># printf returns a value.
# >#
# ># When a function returns a value, and the programmer ignores the code, I
# ># would want to know why. There are usually two explanations: The
# ># programmer made a mistake ignoring the return value, or the programmer
# ># did ignore the return value on purpose. When you write
# >
# > I routinely notice lots of people checking the return of printf
# > instead of simply looking at the terminal window.
#
# Of course, sometimes just staring at terminals may be deceptive at
# times. Consider a program that prints whitespace separated words and
# shows two words on your terminal:
#
# hello world
#
# Have these been printed by a single call to printf?
#
# printf("hello world\n");
#
# Two successive calls?
#
# printf("hello ");
# printf("world\n");

What does that have to do with error checking?

# Or maybe three calls, where the middle one failed, for some obscure
# (and undetected by just staring at the output) reason?
#
# printf("hello ");
# printf("strange ");
# printf("world\n");

For some absurd reason if I see the above code in the .c but I don't see
"strange " in the output, I manage to realize something went wrong without
checking anything else.

I guess I have ESP.


At best the return can indicate something went wrong. To get the details
you'll need more information. Oft times, you get more information by
printfing to the terminal window.
 
S

SM Ryan

# > Also, if the programmer is being particularily careful to check
# > error conditions at every place errors can occur, then
# > a (void) on the printf() can be a signal that the lack of error
# > checking on that one statement is a deliberate design decision
# > instead of something that was overlooked.
#
# Interesting. I knew things like successful file openings should be
# error checked, and pointers should be checked before accessing, etc.
# But printfs? What is usually the accepted degree of error checking?

What I'm wonderring is how do you report an error if printf doesn't
work? I usually use printf or fprintf or perror to print errors. If
the printf doesn't work how do you report the printf doesn't work?
 
G

Gordon Burditt

What I'm wonderring is how do you report an error if printf doesn't
work? I usually use printf or fprintf or perror to print errors. If
the printf doesn't work how do you report the printf doesn't work?

Use printf(). The rule here is that *IF* the problem is running out
of disk space, that you have to keep reporting the error so often
that even if some disk space is freed up, it's immediately used again.
This is called the error-retentive approach, and any resemblance to
anal-retentive is strictly non-coincidental.

For POSIX fans:

while (printf(...error message goes here...) < 0){
fork();
}

Gordon L. Burditt
 
S

S.Tobias

What does that have to do with error checking?

printf("do not ");
printf("fire weapon\n");
For some absurd reason if I see the above code in the .c but I don't see
"strange " in the output, I manage to realize something went wrong without
checking anything else.

Who says you'll be able to see the output?
 
S

SM Ryan

(e-mail address removed) (Gordon Burditt) wrote:
# >What I'm wonderring is how do you report an error if printf doesn't
# >work? I usually use printf or fprintf or perror to print errors. If
# >the printf doesn't work how do you report the printf doesn't work?
#
# Use printf(). The rule here is that *IF* the problem is running out
# of disk space, that you have to keep reporting the error so often
# that even if some disk space is freed up, it's immediately used again.
# This is called the error-retentive approach, and any resemblance to
# anal-retentive is strictly non-coincidental.
#
# For POSIX fans:
#
# while (printf(...error message goes here...) < 0){
# fork();
# }

On some kernels you need a sleep in there. Some kernel abort processes
if there are too many forks too quickly.
 
S

SM Ryan

#
# > > Two successive calls?
# > >
# > > printf("hello ");
# > > printf("world\n");
#
# > What does that have to do with error checking?
#
# printf("do not ");
# printf("fire weapon\n");

The navy has a practice that you repeat orders back to verify they've been
heard correctly. If you're doing safety critical programming, merely trusting
the return of printf is stupid. You would have to add extra layers of
read back and feedback to verify correct transmission.

Unless you're debugging a program error, testing the return of printf is
either overkill (because the output is its own trace), or woefully inadequate.

# > > Or maybe three calls, where the middle one failed, for some obscure
# > > (and undetected by just staring at the output) reason?
# > >
# > > printf("hello ");
# > > printf("strange ");
# > > printf("world\n");
#
# > For some absurd reason if I see the above code in the .c but I don't see
# > "strange " in the output, I manage to realize something went wrong without
# > checking anything else.
#
# Who says you'll be able to see the output?

Who says I won't? And if the disk has a track limit, the operating
system lets me know. If I'm really doing critical operations and
need to know precise status, I would use write() so that I can
skip bufferring effects and know precisely what the kernel has
gotten and when it has gotten it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top