473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

EOF from printf

When "printf" function returns "EOF"? Will "printf" ever fail?

Mar 27 '07 #1
20 2302
"v4vijayakumar" <vi******************@gmail.comwrites:
When "printf" function returns "EOF"? Will "printf" ever fail?
C99 7.19.6.3p3:

The printf function returns the number of characters transmitted,
or a negative value if an output or encoding error occurred.

The negative value isn't necessarily the same as EOF, and you
shouldn't assume that it is.

Any decent C textbook, or your system's documentation, should answer
this for you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 27 '07 #2
On Mar 27, 1:12 pm, Keith Thompson <k...@mib.orgwrote:
"v4vijayakumar" <vijayakumar.subbu...@gmail.comwrites:
When "printf" function returns "EOF"? Will "printf" ever fail?

C99 7.19.6.3p3:

The printf function returns the number of characters transmitted,
or a negative value if an output or encoding error occurred.

The negative value isn't necessarily the same as EOF, and you
shouldn't assume that it is.
Thanks. I tried, "man 3 printf" in cygwin. It says,

---
RETURNS
`sprintf' and `asprintf' return the number of bytes in
the output
string, save that the concluding `NULL' is not counted.
`printf' and
`fprintf' return the number of characters transmitted. If
an error
occurs, `printf' and `fprintf' return `EOF' and `asprintf'
returns -1.
No error returns occur for `sprintf'.
---

I just wanted to know, in which cases it can fail.

Why it returns negative value, when "EOF" would suffice? Does the
negative
return value has any more information for the failure?

Mar 27 '07 #3

"v4vijayakumar" <vi******************@gmail.comwrote in message
news:11********************@r56g2000hsd.googlegrou ps.com...
When "printf" function returns "EOF"? Will "printf" ever fail?
On some systems printf causes data to be printed on a serial line. printf
doesnt need to print to a computer screen, on some systems it can be really
useful to test for the return value of printf
Mar 27 '07 #4
"v4vijayakumar" <vi******************@gmail.comwrote in message
When "printf" function returns "EOF"? Will "printf" ever fail?
Not printf() never fails. It is like my Volvo. It has never been for a
repair in over five years.

Seriously, there are all sorts of things that could possibly go wrong with
printf(), such as the operating system running out of memory, or the
terminal crashing. But on a modern system they won't and the possibility can
be discounted.

If you are running on some sort of 4-bit digital watcvh with a teletype
attrached for debugging it might be a different story.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Mar 27 '07 #5
Malcolm McLean wrote:
"v4vijayakumar" <vi******************@gmail.comwrote in message
When "printf" function returns "EOF"? Will "printf" ever fail?
Not printf() never fails. It is like my Volvo. It has never been for a
repair in over five years.

Seriously, there are all sorts of things that could possibly go wrong with
printf(), such as the operating system running out of memory, or the
terminal crashing. But on a modern system they won't and the possibility can
be discounted.

If you are running on some sort of 4-bit digital watcvh with a teletype
attrached for debugging it might be a different story.
On modern systems, printf can easily fail when output is not directed
to any sort of terminal. An easy example is running out of disk space.

Mar 27 '07 #6
v4vijayakumar wrote:
>
When "printf" function returns "EOF"? Will "printf" ever fail?
When "printf" returns EOF the function has already failed. From
N869:

[#14] The fprintf function returns the number of characters
transmitted, or a negative value if an output or encoding
error occurred.

However, it may never return EOF.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 27 '07 #7
Malcolm McLean wrote, On 27/03/07 20:42:
"v4vijayakumar" <vi******************@gmail.comwrote in message
>When "printf" function returns "EOF"? Will "printf" ever fail?
Not printf() never fails. It is like my Volvo. It has never been for a
repair in over five years.

Seriously, there are all sorts of things that could possibly go wrong
with printf(), such as the operating system running out of memory, or
the terminal crashing. But on a modern system they won't and the
possibility can be discounted.

If you are running on some sort of 4-bit digital watcvh with a teletype
attrached for debugging it might be a different story.
Or someone has redirected standard output to a file and the disk has
filed up (this does happen). Or standard output is redirected to a
TCP/IP port and the connection has disappeared (this does happen). Or
someone is running the program using a remote shell and the connection
has dropped (this does happen). I'm sure if I tried I could come up with
more examples where printf would fail, these are just a few REAL
examples where printf CAN fail on a MODERN system.
--
Flash Gordon
Mar 27 '07 #8
CBFalconer <cb********@yahoo.comwrites:
v4vijayakumar wrote:
>When "printf" function returns "EOF"? Will "printf" ever fail?

When "printf" returns EOF the function has already failed.
....
However, it may never return EOF.
To clarify for others: this final statement is almost certainly
intended to mean

It is possible for printf() to never return EOF.

(There are many other negative values it could return, all of which
indicate occurrence of an error.)
(Chuck's final statement _could_ be read as

It is not possible for printf() to return EOF.

This reading would be incorrect, but the ambiguity of "may never VERB"
leads as validly to this as to the previous parsing.)

mlp
Mar 28 '07 #9
"v4vijayakumar" <vi******************@gmail.comwrote:
On Mar 27, 1:12 pm, Keith Thompson <k...@mib.orgwrote:
"v4vijayakumar" <vijayakumar.subbu...@gmail.comwrites:
When "printf" function returns "EOF"? Will "printf" ever fail?
C99 7.19.6.3p3:

The printf function returns the number of characters transmitted,
or a negative value if an output or encoding error occurred.

The negative value isn't necessarily the same as EOF, and you
shouldn't assume that it is.

Thanks. I tried, "man 3 printf" in cygwin.
That gives you the Ganuck definition of printf(), which may include
more, or even fewer, restrictions than the ISO C one.
I just wanted to know, in which cases it can fail.
Any number of causes which can stop a program from reading a file. For
example, if the file was on a USB stick and you just pulled it out; or
if the disk has a read error; well, you name it, really.
Why it returns negative value, when "EOF" would suffice?
Contrariwise, why demand that it return EOF (not "EOF"; printf() returns
an int, so it cannot possibly return "EOF", which is a string literal),
when allowing it to return any negative value works fine?

Richard
Mar 28 '07 #10
rl*@hoekstra-uitgeverij.nl (Richard Bos) wrote:
"v4vijayakumar" <vi******************@gmail.comwrote:
Thanks. I tried, "man 3 printf" in cygwin.

That gives you the Ganuck definition of printf(), which may include
more, or even fewer, restrictions than the ISO C one.
I just wanted to know, in which cases it can fail.

Any number of causes which can stop a program from reading a file. For
example, if the file was on a USB stick and you just pulled it out; or
if the disk has a read error; well, you name it, really.
Erm. s/reading/writing/ and s/read/write/, obviously. The point being,
that printf() prints to stdout, which may be a screen, but also may be
any other default output device, including (possibly the most often used
alternative) files. But never an input file... *Slaps self*

Richard
Mar 28 '07 #11
Mark L Pappin wrote:
CBFalconer <cb********@yahoo.comwrites:
>v4vijayakumar wrote:
>>When "printf" function returns "EOF"? Will "printf" ever fail?

When "printf" returns EOF the function has already failed.
...
>However, it may never return EOF.

To clarify for others: this final statement is almost certainly
intended to mean

It is possible for printf() to never return EOF.

(There are many other negative values it could return, all of
which indicate occurrence of an error.)
Correct. I suspect this is an outgrowth of existing practice in
the days of preparing C89. EOF satisfies the requirements of
printf, but there probably existed systems that did otherwise. So
you have to apply the < 0 test.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 28 '07 #12
Mark L Pappin wrote:
CBFalconer <cb********@yahoo.comwrites:
v4vijayakumar wrote:
When "printf" function returns "EOF"? Will "printf" ever fail?
When "printf" returns EOF the function has already failed.
...
However, it may never return EOF.

To clarify for others: this final statement is almost certainly
intended to mean

It is possible for printf() to never return EOF.

(There are many other negative values it could return, all of which
indicate occurrence of an error.)
(Chuck's final statement could be read as

It is not possible for printf() to return EOF.

This reading would be incorrect, but the ambiguity of "may never VERB"
leads as validly to this as to the previous parsing.)
I think "it might never return EOF" would be less ambiguous.


Brian
Mar 28 '07 #13
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"v4vijayakumar" <vi******************@gmail.comwrote:
[...]
>Thanks. I tried, "man 3 printf" in cygwin.

That gives you the Ganuck definition of printf(), which may include
more, or even fewer, restrictions than the ISO C one.
A mostly off-topic quibble:

I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
gcc definition of printf. In Cygwin, the proided printf is part of
glibc, which is a separate package.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 28 '07 #14
Keith Thompson <ks***@mib.orgwrites:
[...]
I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
gcc definition of printf. In Cygwin, the proided printf is part of
glibc, which is a separate package.
I meant "provided", of course.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 28 '07 #15
Keith Thompson wrote:
Keith Thompson <ks***@mib.orgwrites:
[...]
>I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
gcc definition of printf. In Cygwin, the proided printf is part of
glibc, which is a separate package.

I meant "provided", of course.
And I was trying to decide between prodded and prouded,

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 29 '07 #16
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"v4vijayakumar" <vi******************@gmail.comwrote:
[...]
Thanks. I tried, "man 3 printf" in cygwin.
That gives you the Ganuck definition of printf(), which may include
more, or even fewer, restrictions than the ISO C one.

A mostly off-topic quibble:

I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
gcc definition of printf. In Cygwin, the proided printf is part of
glibc, which is a separate package.
Quibble indeed. By "Ganuck", I mean the-not-quite-C-implementation-
provided-by-the-Ganoo-crowd, and the language compiled by default by
that implementation. gcc is only part of that; glibc is another part.
Insisting that they be taken separately is just another tool for Ganoo
to play the embrace-and-extend game, and when that game goes wrong, to
put the blame on another party. I will not play along with that.

Richard
Mar 29 '07 #17
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"v4vijayakumar" <vi******************@gmail.comwrote:
[...]
>Thanks. I tried, "man 3 printf" in cygwin.

That gives you the Ganuck definition of printf(), which may include
more, or even fewer, restrictions than the ISO C one.

A mostly off-topic quibble:

I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
gcc definition of printf. In Cygwin, the proided printf is part of
glibc, which is a separate package.

Quibble indeed. By "Ganuck", I mean the-not-quite-C-implementation-
provided-by-the-Ganoo-crowd, and the language compiled by default by
that implementation. gcc is only part of that; glibc is another part.
Insisting that they be taken separately is just another tool for Ganoo
to play the embrace-and-extend game, and when that game goes wrong, to
put the blame on another party. I will not play along with that.
<OT>
Insisting that they be taken separately simply reflects reality. The
gcc compiler is commonly used with runtime libraries other than glibc;
in some cases, compilers other than gcc are used with glibc.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 29 '07 #18
In article <46****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
....
>Quibble indeed. By "Ganuck", I mean the-not-quite-C-implementation-
provided-by-the-Ganoo-crowd, and the language compiled by default by
that implementation.
Quibble: ITYM "I mean the-more-than-C-implementation..."

Other than that, your point is well taken.

Mar 29 '07 #19
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
A mostly off-topic quibble:

I assume that "Ganuck" is meant to refer to gcc. In fact, there is no
gcc definition of printf. In Cygwin, the proided printf is part of
glibc, which is a separate package.
Quibble indeed. By "Ganuck", I mean the-not-quite-C-implementation-
provided-by-the-Ganoo-crowd, and the language compiled by default by
that implementation. gcc is only part of that; glibc is another part.
Insisting that they be taken separately is just another tool for Ganoo
to play the embrace-and-extend game, and when that game goes wrong, to
put the blame on another party. I will not play along with that.

<OT>
Insisting that they be taken separately simply reflects reality. The
gcc compiler is commonly used with runtime libraries other than glibc;
in some cases, compilers other than gcc are used with glibc.
</OT>
<Not OT>

That's all very well, but the ISO C Standard, which is the subject of
this group, only cares about implementations. What those implementations
are made of is the problem of the implementor. If you build a chimaeric
implementation which proves not to be conforming, that's your problem,
not mine.

Richard
Apr 5 '07 #20
Richard Bos wrote:
>
.... snip ...
>
That's all very well, but the ISO C Standard, which is the subject
of this group, only cares about implementations. What those
implementations are made of is the problem of the implementor. If
you build a chimaeric implementation which proves not to be
conforming, that's your problem, not mine.
However detecting, and taming, those implementations is topical
IMO.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Apr 7 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
by: whatluo | last post by:
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...
4
by: pai | last post by:
Hi , Can any one tell me how this statement of printf is behaving . how the last digit is printed int a=2,b=4,c=7; printf("%d",printf("%d %d:",a,b)); //answer to this was 2 4:3
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
19
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement,...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.