473,513 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11139
"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
15207
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
3193
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
2616
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
3435
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
3906
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
3130
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
11690
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
7822
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
30178
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
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.