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

EOF in Windows

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
Nov 14 '05 #1
11 11134
"Mazen S. Alzogbi" <ma*********@alzogbi.name> wrote in message
news:51**************************@posting.google.c om...
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 <stdio.h>
main() {
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
Nov 14 '05 #2
ma*********@alzogbi.name (Mazen S. Alzogbi) wrote:
Hi,

I am a C beginner and I am trying to execute the following code from
K&R book:
Add this line:
#include <stdio.h>
main() {
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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #3
> 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
Nov 14 '05 #4
> 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
Nov 14 '05 #5
> 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
Nov 14 '05 #6
j

"Mazen S. Alzogbi" <ma*********@alzogbi.name> wrote in message
news:51*************************@posting.google.co m...
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?


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/publ...ginner_s_c.htm

Thanks again. Cheers!

Mazen

Nov 14 '05 #7
ma*********@alzogbi.name (Mazen S. Alzogbi) wrote in message news:<51**************************@posting.google. com>...
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 hugo27, July 02, 2004
Questions & Comments:
double nc; /* is a type of float declaration, yah? */
What does it mean to initialize nc to an int?
Does that make it proper to ++ increment nc?
The for statement ends with semicolon(;).
Far as I know this is NOT OK.
The for statement line should end with the closing )
or braces, i.e. {block} .

The printf format "%0.f\n" would just print 0.00000
on new line over and over(if it executes at all)
since nc is always x.000000.

hugo

Nov 14 '05 #8

"hugo27" <ob****@yahoo.com> wrote in message
news:a7**************************@posting.google.c om...
ma*********@alzogbi.name (Mazen S. Alzogbi) wrote in message

news:<51**************************@posting.google. com>...
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

hugo27, July 02, 2004
Questions & Comments:
double nc; /* is a type of float declaration, yah? */
What does it mean to initialize nc to an int?
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'.

Does that make it proper to ++ increment nc?
The increment and decrement operators are valid for all
numeric types. They add and subtract the value one
from their operand.
The for statement ends with semicolon(;).
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.
Far as I know this is NOT OK.
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. :-)
The for statement line should end with the closing )
or braces, i.e. {block} .
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'.
The printf format "%0.f\n" would just print 0.00000
on new line over and over(if it executes at all)
It will execute if/when 'getchar()' returns EOF.
since nc is always x.000000.


'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
Nov 14 '05 #9
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:_9*****************@newsread2.news.pas.earthl ink.net...

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
Nov 14 '05 #10
In article <51**************************@posting.google.com >,
ma*********@alzogbi.name says...
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?


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

Nov 14 '05 #11
"Mike Wahler" <mk******@mkwahler.net> wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote:

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).


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

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #12

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

Similar topics

2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
7
by: lvpaul | last post by:
Hallo ! I am using IIS-Windows-Authentication in my intranet (web.config <authentication mode="Windows" /> <identity impersonate="true" /> How can I get the users (client) IP-Address ? I...
7
by: Tyler Foreman | last post by:
Hello, I have a strange problem that occurs every so often in my application. It usually takes place when I hide one form and activate another. What happens is I get the following exception:...
1
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm...
0
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server -...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
2
by: sambo251 | last post by:
After running a few updates I get this very annoying "Windows Installer" error #1706 that will ne go away! It keeps saying that it cannot find the file "instantsharedevices.msi", that it is on...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...

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.