Does scanf ever return EOF?

H

hugo27

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?
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
B

Barry Schwarz

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>>
 
J

Joe Wright

hugo27 said:
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.
 
H

hugo27

Joe Wright said:
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?
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hugo27 wrote:
|
|>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-----
 
P

Peter Shaggy Haywood

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"?
 
D

Dan Pop

In said:
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
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top