printf return Q

B

Bill Cunningham

Since printf returns the number of characters without the '\0' how do
people check printf's return value for errors in the mainstream. My idea
since I'm dealing with unsigned ints is simply to count the characters. For
example,

int pe;
unsigned int variable;

if ((pe=printf("%.2u\n",variable))==0
return -1;


Would that take care of all kinds of returns ? What about negative returns?
I've never done error checking with printf before.

B
 
B

Bill Cunningham

Bill said:
Since printf returns the number of characters without the '\0' how
do people check printf's return value for errors in the mainstream (C
community).
My idea since I'm dealing with unsigned ints is simply to count the
characters. For example,

int pe;
unsigned int variable;

if ((pe=printf("%.2u\n",variable))==0)
return -1;

Oops forgot that ending )
 
J

Jorgen Grahn

Oops forgot that ending )

My manual says "if an output error is encountered, a negative value is
returned", so your code above is incorrect. Returning 0 is part of the
normal non-error return values, e.g. for printf("%s", "");

/Jorgen
 
B

Bill Cunningham

Jorgen said:
My manual says "if an output error is encountered, a negative value is
returned", so your code above is incorrect. Returning 0 is part of the
normal non-error return values, e.g. for printf("%s", "");

My man page says if I am remembering correctly without going back and
looking that a negative value can be encountered without an error. I'll have
to go back and verify.

B
 
B

Bill Cunningham

Jorgen Grahn wrote:

[snip]
My manual says "if an output error is encountered, a negative value is
returned", so your code above is incorrect. Returning 0 is part of the
normal non-error return values, e.g. for printf("%s", "");

/Jorgen

Yep. You're right. I guess if I was paying attention like I should've
been I would've saw that. Sorry for the alarm.

B
 
G

Geoff

Since printf returns the number of characters without the '\0' how do
people check printf's return value for errors in the mainstream. My idea
since I'm dealing with unsigned ints is simply to count the characters. For
example,

int pe;
unsigned int variable;

if ((pe=printf("%.2u\n",variable))==0
return -1;


Would that take care of all kinds of returns ? What about negative returns?
I've never done error checking with printf before.

B

The printf function returns the number of characters printed. Since \0
is never "printed" you don't need to say that. Printf returns a
negative value if there was an error and it sets errno. You don't
really need to assign a variable to receive the value.

You can write:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
..
..
..
unsigned int variable;

if (printf("%.2u\n", variable) < 0)
exit (errno);

Note: variable is being used without being initialized.

The problem with handling an error from printf is how to notify the
user. If you can't use printf, you can't printf a message to the user.
 
K

Keith Thompson

Geoff said:
The printf function returns the number of characters printed. Since \0
is never "printed" you don't need to say that. Printf returns a
negative value if there was an error and it sets errno. You don't
really need to assign a variable to receive the value.

printf might set errno in some implementations, but the standard
doesn't guarantee it. Just check the returned value.
You can write:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
.
.
.
unsigned int variable;

if (printf("%.2u\n", variable) < 0)
exit (errno);

The value of errno isn't intended to be returned to the environment as
an exit status. It makes more sense to print an error message (perhaps
using perror()) and then call exit(EXIT_FAILURE).
Note: variable is being used without being initialized.

The problem with handling an error from printf is how to notify the
user. If you can't use printf, you can't printf a message to the user.

If printing to stdout fails, printing to stderr (which is where error
messages should go anyway) might still succeed.
 
B

Bill Cunningham

Geoff said:
The printf function returns the number of characters printed. Since \0
is never "printed" you don't need to say that. Printf returns a
negative value if there was an error and it sets errno. You don't
really need to assign a variable to receive the value.

You can write:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
.
.
.
unsigned int variable;

if (printf("%.2u\n", variable) < 0)
exit (errno);

So is errno a macro. I've never used it before. I would try this if a
user needs to know what has failed and something other than another function
simply needing to see the negative number isn't enough...


if (printf("%.2u\n",variable)<0){
fputs("printf error\n",stderr);
return -1;}

B
 
A

Anders Wegge Keller

Geoff said:
The printf function returns the number of characters printed. Since
\0 is never "printed" you don't need to say that. Printf returns a
negative value if there was an error and it sets errno.

Are you sure about that? Some implementations may do so, but I don't
see a requirement in the standard.

...
The problem with handling an error from printf is how to notify the
user. If you can't use printf, you can't printf a message to the
user.

Malformed format strings could lead to an error condition, where a
simple construction like

fprintf (stderr, "Format error.\n");

would still work.
 
A

Anders Wegge Keller

I've been used to a zero return meaning "success" and anything else
being an error. I've just looked at a couple of manuals (here and
online) and I find that being the case for most of them. But I also
found one that says the return value is either the number of
characters printed or negative if there's an error. So I guess,
with that library, zero indicates success, but no characters
printed?

My question is about "and it sets errno".
I don't think I've ever seen any code where the return value if
printf is checked.

If you include the whole family, I check the return value quite
often. But printf() specific ... I don't think I've ever worried about
that, but I won't claim that it hasn't got uses. I can easily imagine
an embedde implementation, where stdout is a buffered serial line. In
that case, printf() could fail if the buffer overflows, or if the
connection to the outside world is lost.
 
G

Geoff

Are you sure about that? Some implementations may do so, but I don't
see a requirement in the standard.
This is comp.lang.c not comp.std.c
...


Malformed format strings could lead to an error condition, where a
simple construction like

fprintf (stderr, "Format error.\n");

would still work.

Are you sure about that? What if the error was ENOMEM?
 
K

Keith Thompson

Geoff said:
This is comp.lang.c not comp.std.c

Yes, this is comp.lang.c. Which discusses the C programming
language. Which is defined by the C standard.

THe difference is that comp.std.c discusses the standard document,
and comp.lang.c discusses the language.

Posting to comp.lang.c is not a license to make mistakes. (Not that
a license is required; I've made plenty myself.)

The fact is that printf() does not necessarily set errno to any
meaningful value on an error.

Also, the man page for printf on my system says nothing about it
setting errno.

What is the basis for your claim that printf sets errno if there's
an error?

[...]
 
J

J. J. Farrell

Geoff said:
The printf function returns the number of characters printed. Since \0
is never "printed" you don't need to say that. Printf returns a
negative value if there was an error and it sets errno.

In C, printf returns the number of characters printed, or returns a
negative value if an output or encoding error occurs. That's all you can
assume for C.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top