473,236 Members | 1,323 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,236 software developers and data experts.

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 7596
-----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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
20
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf...
16
by: Mars | last post by:
I am reading the code of a program from the examples, but really don't understand this one........ int i,j; ..................................................... while (scanf("%d",&i),j) {...
7
by: happy | last post by:
I need to write a string field in a structure data type to a file using fprintf,fscanf. I failed to use the below syntax. Please I need to not go outside scanf,fprintf,printf . /* Book name ...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
6
by: alternativa | last post by:
Hi, I have problem with the following function - it was intended to ask a user for a 4-digits number: double ask_for_number (void) { char *notint; char s2; double entered_number;
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
8
by: Army1987 | last post by:
Is this a good way to discard unread data after scanf()? while (getchar() != '\n') ; According to the FAQ scanf always leaves the trailing newline on the input stream, so there's no risk of...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.