EOF in Windows

Discussion in 'C Programming' started by Mazen S. Alzogbi, Jul 2, 2004.

  1. 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
     
    Mazen S. Alzogbi, Jul 2, 2004
    #1
    1. Advertising

  2. Mazen S. Alzogbi

    Mike Wahler Guest

    "Mazen S. Alzogbi" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Jul 2, 2004
    #2
    1. Advertising

  3. (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 ()
    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
     
    Irrwahn Grausewitz, Jul 2, 2004
    #3
  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
     
    Mazen S. Alzogbi, Jul 2, 2004
    #4
  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
     
    Mazen S. Alzogbi, Jul 2, 2004
    #5
  6. > 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
     
    Mazen S. Alzogbi, Jul 2, 2004
    #6
  7. Mazen S. Alzogbi

    j Guest

    "Mazen S. Alzogbi" <> wrote in message
    news:...
    > > 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/public/reviews/0sb/beginner_s_c.htm


    > Thanks again. Cheers!
    >
    > Mazen
     
    j, Jul 2, 2004
    #7
  8. Mazen S. Alzogbi

    hugo27 Guest

    (Mazen S. Alzogbi) wrote in message news:<>...
    > 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
     
    hugo27, Jul 2, 2004
    #8
  9. Mazen S. Alzogbi

    Mike Wahler Guest

    "hugo27" <> wrote in message
    news:...
    > (Mazen S. Alzogbi) wrote in message

    news:<>...
    > > 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
     
    Mike Wahler, Jul 3, 2004
    #9
  10. Mazen S. Alzogbi

    Mike Wahler Guest

    "Mike Wahler" <> wrote in message
    news:_9mFc.2968$...
    >
    > 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
     
    Mike Wahler, Jul 3, 2004
    #10
  11. Mazen S. Alzogbi

    Randy Howard Guest

    In article <>,
    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
     
    Randy Howard, Jul 3, 2004
    #11
  12. "Mike Wahler" <> wrote:
    >"Mike Wahler" <> 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 ()
    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
     
    Irrwahn Grausewitz, Jul 4, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sid
    Replies:
    1
    Views:
    602
  2. Dmitry Anikin
    Replies:
    2
    Views:
    7,523
    Peter Hansen
    Apr 3, 2006
  3. Kobu
    Replies:
    10
    Views:
    628
    Keith Thompson
    Mar 4, 2005
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    697
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,066
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page