EOF in Windows

  • Thread starter Mazen S. Alzogbi
  • Start date
M

Mazen S. Alzogbi

Hi,

I am a C beginner and I am trying to execute the following code from
K&R book:

main() {
double nc;

for(nc =0; getchar() != EOF; ++nc);
printf("%0.f\n", nc);
}

I am running a Win2k OS and this is a console application. What should
I type to get the EOF value in getchar() and get the program to
terminate normally? In UNIX that is CTRL+D but it's not working with
Win2k.

Thanks,

Mazen
 
M

Mike Wahler

Mazen S. Alzogbi said:
Hi,

I am a C beginner and I am trying to execute the following code from
K&R book:

Note that K&R C is nonstandard. We try to stick to standard
code here (although K&R (2nd ed) is still a highly recommended
book on C). I suggest you use standard compliant code, as
indicated by my corrections and additions:


#include said:

int main()
double nc;

for(nc =0; getchar() != EOF; ++nc);
printf("%0.f\n", nc);

return 0;
I am running a Win2k OS and this is a console application. What should
I type to get the EOF value in getchar() and get the program to
terminate normally? In UNIX that is CTRL+D but it's not working with
Win2k.

This isn't really topical here, as it's a question about your
operating system, and not the C language.

I haven't used Win2K, but most of its predecessors and DOS, where
EOF is indicated from the keyboard with CTRL+Z. You may or may not
also need to press "Enter" after the CTRL-Z. If that doesn't work,
you'll need to check your OS documentation, or ask in a Windows
group.

-Mike
 
I

Irrwahn Grausewitz

Hi,

I am a C beginner and I am trying to execute the following code from
K&R book:

Add this line:
#include said:

Better:
int main(void) {
double nc;

for(nc =0; getchar() != EOF; ++nc);
printf("%0.f\n", nc);

Add:
return 0;
}

I am running a Win2k OS and this is a console application. What should
I type to get the EOF value in getchar() and get the program to
terminate normally? In UNIX that is CTRL+D but it's not working with
Win2k.

<Off-topic>
Enter Ctrl-Z *at the beginning of a line*.
</Off topic>

Regards
 
M

Mazen S. Alzogbi

Note that K&R C is nonstandard. We try to stick to standard
code here (although K&R (2nd ed) is still a highly recommended
book on C). I suggest you use standard compliant code, as
indicated by my corrections and additions:
-Mike

Hi Mike,

Thanks for your help. Ctrl+Z was the trick. I am reading the 2nd
edition of K&R and I thought the phrase "ANSI C Compliant" on the
cover meant that this is standard C, isn't?

I would appreciate if you mention some 5 stars titles for a C beginner
to start with. Please note that I am not a total beginner, I practiced
C for 3 years, stopped for 6 and now I am back agian. So what I really
need is a fast refresh to my information.

Thanks again. Cheers!

Mazen
 
M

Mazen S. Alzogbi

Note that K&R C is nonstandard. We try to stick to standard
code here (although K&R (2nd ed) is still a highly recommended
book on C). I suggest you use standard compliant code, as
indicated by my corrections and additions:
-Mike

Hi Mike,

Thanks for your help. Ctrl+Z was the trick. I am reading the 2nd
edition of K&R and I thought the phrase "ANSI C Compliant" on the
cover meant that this is standard C, isn't?

I would appreciate if you mention some 5 stars titles for a C beginner
to start with. Please note that I am not a total beginner, I practiced
C for 3 years, stopped for 6 and now I am back agian. So what I really
need is a fast refresh to my information.

Thanks again. Cheers!

Mazen
 
M

Mazen S. Alzogbi

Note that K&R C is nonstandard. We try to stick to standard
code here (although K&R (2nd ed) is still a highly recommended
book on C). I suggest you use standard compliant code, as
indicated by my corrections and additions:
-Mike

Hi Mike,

Thanks for your help. Ctrl+Z was the trick. I am reading the 2nd
edition of K&R and I thought the phrase "ANSI C Compliant" on the
cover meant that this is standard C, isn't?

I would appreciate if you mention some 5 stars titles for a C beginner
to start with. Please note that I am not a total beginner, I practiced
C for 3 years, stopped for 6 and now I am back agian. So what I really
need is a fast refresh to my information.

Thanks again. Cheers!

Mazen
 
J

j

Mazen S. Alzogbi said:
Hi Mike,

Thanks for your help. Ctrl+Z was the trick. I am reading the 2nd
edition of K&R and I thought the phrase "ANSI C Compliant" on the
cover meant that this is standard C, isn't?

Your original post only mentioned ``K&R book''
People are unable to discern whether that is the first edition or
second edition. (We aren't psychics)

Generally you used ``k&r2'' to refer to the second edition.
k&r2 teaches c89 but note to also go over the errata:
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html
I would appreciate if you mention some 5 stars titles for a C beginner
to start with. Please note that I am not a total beginner, I practiced
C for 3 years, stopped for 6 and now I am back agian. So what I really
need is a fast refresh to my information.

See the accu:
http://www.accu.org/bookreviews/public/reviews/0sb/beginner_s_c.htm
 
H

hugo27

Hi,

I am a C beginner and I am trying to execute the following code from
K&R book:

main() {
double nc;

for(nc =0; getchar() != EOF; ++nc);
printf("%0.f\n", nc);
}

I am running a Win2k OS and this is a console application. What should
I type to get the EOF value in getchar() and get the program to
terminate normally? In UNIX that is CTRL+D but it's not working with
Win2k.

Thanks,

Mazen
 
M

Mike Wahler

hugo27 said:
(e-mail address removed) (Mazen S. Alzogbi) wrote in message

First note that OP's code did not initialize 'nc',
but assigned to it later.

"Initialize nc to an int" doesn't mean anything.

The statement:

double nc = 0;

initializes 'nc' with a 'double precision' floating point value of zero.

The statement;

nc = 0;

assigns a 'double precision' floating point value of zero to 'nc'.


The increment and decrement operators are valid for all
numeric types. They add and subtract the value one
from their operand.

I may be mistaken, but I think that's intentional.
The program will report how many characters were
input before EOF. Otherwise it would simply output
a sequence of numbers beginning with 1. Since OP
didn't state the code's intent, of course I'm only
guessing.

Depends what you mean by "OK". It's perfectly valid syntax,
but often not what the coder intended. I'm still waiting for
a mind-reading compiler. :)

Depends upon the intention. Sometimes all the desired
processing is done within the parentheses, e.g a function
to discard 'extraneous' input:

int c = 0;
while((c = getchar()) != EOF && c != '\n');

I do that sometimes, but I'll put the semicolon on a separate
line with a comment for clarity:

while((c = getchar()) != EOF && c != '\n')
; /* null statement */

Also note that statements (except preprocessor directives)
are not required to be separated by newlines, e.g. this is valid:

int main() { return 0; }

Also not all of a statement must appear on the same line:

for(
i = 0;
i <
10;
++i
)
{ printf("%d\n",
i)
;}

C's syntax is considered 'free form'.

It will execute if/when 'getchar()' returns EOF.

'nc' will be incremented every time 'getchar()' extracts
character from the standard input. So at 'for' loop
termination, it may be zero, or some greater value.

Try compiling and running the program. At the input prompt,
type in e.g. "Hello", and see what it does.


-Mike
 
M

Mike Wahler

Mike Wahler said:
Try compiling and running the program. At the input prompt,
type in e.g. "Hello",

(followed by whatever keystroke sequence your OS defines
for EOF).

-Mike
 
R

Randy Howard

Hi Mike,

Thanks for your help. Ctrl+Z was the trick. I am reading the 2nd
edition of K&R and I thought the phrase "ANSI C Compliant" on the
cover meant that this is standard C, isn't?

Yes. Your original post said just "K&R", which was published prior
to the ANSI C standard, hence his response. "K&R2" is the widely
accepted nickname for the book you have. Also keep in mind that
the book was written a long time ago, and general practice, subsequent
standards, and compiler behavior has changed since then. The latest
current C Standard is actually an ISO (not ANSI) standard, referred
to generally as C99. It is available from the ISO website in PDF form,
or can be purchased in book form, with some updates applied, as
published by Wiley.
I would appreciate if you mention some 5 stars titles for a C beginner
to start with.

K&R2 is an excellent book, although beginners sometimes find it to be
"dense" in that there is a lot of material, but not a lot of explanation.
For a book that conveys so much information, it is remarkably thin. It
is in a sense, the "bible" for C programmers. Many other books are
available, some good, some downright evil, and almost all are much more
verbose. "C, A Reference Manual" by Harbison and Steele is another good
reference book, but note that it does not teach C programming, but rather
does a good job of explaining the standard(s) as well as the behavior of
the standard library functions.

By far, the best general "refresher" for someone trying to get back into
C programming is probably the comp.lang.c FAQ which is posted to this
newsgroup regularly. http://www.eskimo.com/~scs/C-faq/faq.html
 
I

Irrwahn Grausewitz

Mike Wahler said:
(followed by whatever keystroke sequence your OS defines
for EOF).

Strangely, some OSs will merrily ignore the EOF, if the sequence
doesn't appear at the start of a new line.

Regards
 

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

Similar Threads

EOF question 41
Beginner at c 0
EOF question.. 2
Question about the getchar() and eof !!!... 25
problem with getchar EOF 2
Detecting EOF 3
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? 67
What's wrong ? 21

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top