468,772 Members | 1,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

Does scanf ever return EOF?

obrhy8 June 18, 2004

Most compilers define EOF as -1. I'm just putting my toes
in the water with a student's model named Miracle C. The
..h documentation of this compiler does state that when
scanf cannot fill any fields it returns EOF.
I have run some tests on scanf and, so far, I've not found
an EOF return.

For example: If the programer formats scanf for an int or
float type, and the user enters a single non-digit char or
a char string, scanf writes nothing, fills no field, returns 0
and exits to program. The keyboard buffer retains entire input.

In this case one can test the scanf return for 0, then scanf
the buffer into a charArray and printf charArray, to see what
was left in buffer by 1st scanf.

In no case did scanf return EOF.
What condition or event then,
would force scanf to return -1?
Nov 14 '05 #1
7 6761
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hugo27 wrote:
obrhy8 June 18, 2004

Most compilers define EOF as -1. I'm just putting my toes
in the water with a student's model named Miracle C. [snip] What condition or event then,
would force scanf to return -1?


scanf() will return EOF (not necessarily -1) when it tries to read past the
end of the input data. /If/ your scanf() format string is correct for the data
entered, scanf() will read the entered data until either it runs out of data
matching the format, or EOF. /If/ your scanf() format string doesn't match the
input data, then scanf() stops at the first mismatch.

What you encountered is one of those conditions that causes the gurus here to
say not to use scanf() unless you /know/ what you are doing.
- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFA04PpagVFX4UWr64RAn/OAJoCGJt5SaLPTGD1m8cwCgK81LtNDwCg7e1L
Hk+6jeLTsRAJJCdToP4El2w=
=fGSM
-----END PGP SIGNATURE-----
Nov 14 '05 #2
On 18 Jun 2004 15:54:53 -0700, ob****@yahoo.com (hugo27) wrote:
obrhy8 June 18, 2004

Most compilers define EOF as -1. I'm just putting my toes
in the water with a student's model named Miracle C. The
.h documentation of this compiler does state that when
scanf cannot fill any fields it returns EOF.
I have run some tests on scanf and, so far, I've not found
an EOF return.

For example: If the programer formats scanf for an int or
float type, and the user enters a single non-digit char or
a char string, scanf writes nothing, fills no field, returns 0
and exits to program. The keyboard buffer retains entire input.

In this case one can test the scanf return for 0, then scanf
the buffer into a charArray and printf charArray, to see what
was left in buffer by 1st scanf.

In no case did scanf return EOF.
What condition or event then,
would force scanf to return -1?


According to K&R II, scanf will return EOF only if end of file or
error occurs prior to any conversion.
<<Remove the del for email>>
Nov 14 '05 #3
hugo27 wrote:
obrhy8 June 18, 2004

Most compilers define EOF as -1. I'm just putting my toes
in the water with a student's model named Miracle C. The
.h documentation of this compiler does state that when
scanf cannot fill any fields it returns EOF.
I have run some tests on scanf and, so far, I've not found
an EOF return.

For example: If the programer formats scanf for an int or
float type, and the user enters a single non-digit char or
a char string, scanf writes nothing, fills no field, returns 0
and exits to program. The keyboard buffer retains entire input.

In this case one can test the scanf return for 0, then scanf
the buffer into a charArray and printf charArray, to see what
was left in buffer by 1st scanf.

In no case did scanf return EOF.
What condition or event then,
would force scanf to return -1?


Return Value
------------

The number of items successfully matched and assigned. If input
ends, or if there is any input failure before the first item is
converted and assigned, `EOF' is returned. Note that literal
characters (including whitespace) in the format string which matched
input characters count as "converted items", so input failure
*after* such characters were read and matched will *not* cause `EOF'
to be returned.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #4
Joe Wright <jo********@comcast.net> wrote in message news:<Qo********************@comcast.com>...
hugo27 wrote:
obrhy8 June 18, 2004

Most compilers define EOF as -1. I'm just putting my toes
in the water with a student's model named Miracle C. The
.h documentation of this compiler does state that when
scanf cannot fill any fields it returns EOF.
I have run some tests on scanf and, so far, I've not found
an EOF return.

For example: If the programer formats scanf for an int or
float type, and the user enters a single non-digit char or
a char string, scanf writes nothing, fills no field, returns 0
and exits to program. The keyboard buffer retains entire input.

In this case one can test the scanf return for 0, then scanf
the buffer into a charArray and printf charArray, to see what
was left in buffer by 1st scanf.

In no case did scanf return EOF.
What condition or event then,
would force scanf to return -1?
Return Value
------------

The number of items successfully matched and assigned. If input
ends, or if there is any input failure before the first item is
converted and assigned, `EOF' is returned. Note that literal
characters (including whitespace) in the format string which matched
input characters count as "converted items", so input failure
*after* such characters were read and matched will *not* cause `EOF'
to be returned.


Obrhy8 june 19, 2004
For all: I just tested Miracle C and EOF IS -1. L. Pitcher:
Perhaps there is some confusion between Keyboard input
and FILE I/O. EOF sure has a meaning in FILE I/O
but I mean here Keyboard buffer input.
When the user keys in data and presses ENTER
scanf can read to the \n code. How is it possible
for scanf to read beyond \n? B. Schwarz and J.
What constitutes such an error?
What does "input faliure" mean?
I have done a number of test on scanf and I find three broad
cases. First, scanf reads everything assigns field(s),
returns number. KB buffer MT
Second, scanf reads part of buffer, assigns field(s),
returns number. KB buffer retains some data.
Third, scanf reads nothing, writes nothing, returns 0.
KB buffer retains all input. I have found NO case where scanf returns EOF (or -1)
ever, in anything like normal operation.
I think maybe a power failure would do it, but
I don't know for sure.

Nov 14 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hugo27 wrote:
| Joe Wright <jo********@comcast.net> wrote in message
news:<Qo********************@comcast.com>...
|
|>hugo27 wrote:
|>
|>>obrhy8 June 18, 2004
|>>
|>>Most compilers define EOF as -1. I'm just putting my toes
|>>in the water with a student's model named Miracle C. The
|>>.h documentation of this compiler does state that when
|>>scanf cannot fill any fields it returns EOF.
|>>I have run some tests on scanf and, so far, I've not found
|>>an EOF return.
|>>
|>>For example: If the programer formats scanf for an int or
|>>float type, and the user enters a single non-digit char or
|>>a char string, scanf writes nothing, fills no field, returns 0
|>>and exits to program. The keyboard buffer retains entire input.
|>>
|>>In this case one can test the scanf return for 0, then scanf
|>>the buffer into a charArray and printf charArray, to see what
|>>was left in buffer by 1st scanf.
|>>
|>>In no case did scanf return EOF.
|>>What condition or event then,
|>>would force scanf to return -1?
|>
|>Return Value
|>------------
|>
|>The number of items successfully matched and assigned. If input
|>ends, or if there is any input failure before the first item is
|>converted and assigned, `EOF' is returned. Note that literal
|>characters (including whitespace) in the format string which matched
|>input characters count as "converted items", so input failure
|>*after* such characters were read and matched will *not* cause `EOF'
|>to be returned.
|
|
|
|
| Obrhy8 june 19, 2004
|
|
|>For all: I just tested Miracle C and EOF IS -1.

In the "Miracle C" compiler, the EOF manifest constant is defined as -1.
However, -1 is not necessarily EOF. The standard only requires that the EOF
macro resolve to a negative integer constant; under a different compiler, EOF
could resolve to -127 or -8 or -65536.

|
|>L. Pitcher:
|>Perhaps there is some confusion between Keyboard input
|>and FILE I/O.

Nope. No confusion. To C, both are the same.

| EOF sure has a meaning in FILE I/O
|>but I mean here Keyboard buffer input.

Nope. end-of-file is a /condition/ that is recognized by the operating
environment and passed on to your program as the unique value EOF. It has
equal meaning in both an interactive and a file I/O mode, both in terms of the
operating environment (that is to say, it signals "no more data") and in terms
of the C programming language (that is to say, C informs your program that the
operating environment has, for whatever reason, determined that there is a "no
more data" condition on the file being read).

Under MSDOS, it is signalled when someone enters ^Z to signal that there is no
more keyboard input. Under Unix, the condition is signalled when someone
enteres the 'eof' char (usually ^D).

|>When the user keys in data and presses ENTER
|>scanf can read to the \n code. How is it possible
|>for scanf to read beyond \n?

EOF is the value returned by the stdio routines when the operating enviroment
tells them that there is no more data to read. If the I/O buffers have been
exhausted before the scanf() is called, then the scanf() invokes (through
implementation defined behaviour) a read of data from the operating
environment. If the operating environment returns more data, then that data is
parsed by scanf() and all is well. However, if the operating environment
returns a 'no more data' indicator, then the stdio support returns the EOF
value. /That's/ how scanf() can read beyond the \n.

|
|>B. Schwarz and J.
|>What constitutes such an error?
|>What does "input faliure" mean?
|>I have done a number of test on scanf and I find three broad
|>cases. First, scanf reads everything assigns field(s),
|>returns number. KB buffer MT
|>Second, scanf reads part of buffer, assigns field(s),
|>returns number. KB buffer retains some data.
|>Third, scanf reads nothing, writes nothing, returns 0.
|>KB buffer retains all input.
|
|
|>I have found NO case where scanf returns EOF (or -1)
|>ever, in anything like normal operation.

If you are using MSDOS, ^Z (that's <ctrl>Z), followed by the return key.
You'll get an EOF value passed to scanf() when it tries to parse this line.


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFA1P9kagVFX4UWr64RAiM8AJ0QOloHGCp7Dzu+HZRU/wqOot/e5QCgmAnk
TkfqXVaJdtyIo4LA226ThkM=
=cZqx
-----END PGP SIGNATURE-----
Nov 14 '05 #6
Groovy hepcat hugo27 was jivin' on 18 Jun 2004 15:54:53 -0700 in
comp.lang.c.
Does scanf ever return EOF?'s a cool scene! Dig it!
Most compilers define EOF as -1. I'm just putting my toes
Opening a C book I just happen to have lying around and turning to
the page on which fscanf() is described, I read this:

-----------------------------------------------------------------
fscanf returns EOF if end of file or an error occurs before any
conversion...
-----------------------------------------------------------------

The moral: you might learn something if you open a book.
in the water with a student's model named Miracle C. The
.h documentation of this compiler does state that when
scanf cannot fill any fields it returns EOF.

I have run some tests on scanf and, so far, I've not found
an EOF return.

For example: If the programer formats scanf for an int or
float type, and the user enters a single non-digit char or
a char string, scanf writes nothing, fills no field, returns 0
and exits to program. The keyboard buffer retains entire input.
So what? End of file or an error did not occur.
In this case one can test the scanf return for 0, then scanf
the buffer into a charArray and printf charArray, to see what
was left in buffer by 1st scanf.

In no case did scanf return EOF.
What condition or event then,
would force scanf to return -1?


Note that EOF does not have to be -1.
See above for the answer.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #7
In <va*********************@news20.bellglobal.com> Lew Pitcher <lp******@sympatico.ca> writes:
If you are using MSDOS, ^Z (that's <ctrl>Z), followed by the return key.
You'll get an EOF value passed to scanf() when it tries to parse this line.


To avoid system dependencies, use sscanf instead of scanf and pass an
empty string as input string:

fangorn:~/tmp 78> cat test.c
#include <stdio.h>

int main()
{
printf("%d\n", sscanf("", "%*d"));
return 0;
}
fangorn:~/tmp 79> gcc test.c
fangorn:~/tmp 80> ./a.out
-1

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by Teh Charleh | last post: by
20 posts views Thread by Sivarn | last post: by
16 posts views Thread by Mars | last post: by
7 posts views Thread by happy | last post: by
33 posts views Thread by Lalatendu Das | last post: by
6 posts views Thread by alternativa | last post: by
14 posts views Thread by main() | last post: by
8 posts views Thread by Army1987 | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.