stopping a while loop

Discussion in 'C Programming' started by Jean Pierre Daviau, Mar 14, 2005.

  1. Hi every C one,

    Which key entry (if there is any) should stop this loop?

    #include <stdio.h>
    int main(){
    int c, getch(void);

    while((c = getch()) != EOF){
    printf("%c\n", c);
    }
    return 0;
    }
     
    Jean Pierre Daviau, Mar 14, 2005
    #1
    1. Advertisements

  2. Well, to be pedantic about it, we don't know. getch() is not defined by
    the ISO C standard, and thus it might return anything it wants. There
    might be no way for it to ever return EOF. On the other hand, it could
    return EOF after every keypress.
    To be less pedantic, suppose getch() works the same way as getchar().
    Then the answer is "it depends on your operating system". For example,
    UNIX uses ^D, Windows uses ^Z, and AmigaOS uses ^\.
     
    Joona I Palaste, Mar 14, 2005
    #2
    1. Advertisements

  3. In fact, there's a function getch() defined as part of the curses
    package available on Unix-like systems (and probably others). The man
    page (at least on one system I just checked) makes no mention of EOF.
    In fact, it's likely to be used in some kind of raw mode in which it's
    not possible to specify EOF (control-D probably comes through as a
    literal control-D character, '\004').

    I think Windows also defines a getch() function that does something
    different. I have no idea whether it ever returns EOF.

    To the OP: The fact that the getch() function had to be declared
    within the program should have been a clue that it's not declared in
    <stdio.h>, and that it's non-standard.

    If you want to write portable code, use fgetc(), getc(), or getchar()
    (your system's documentation should tell you how they differ). If
    you're curious about getch(), that's a system-specific question; if
    you can't find answers in your system's documentation, try a
    system-specific newsgroup.
     
    Keith Thompson, Mar 14, 2005
    #3
  4. Thak you.


    "Keith Thompson" <> a écrit dans le message de
    ...
    | > Jean Pierre Daviau <> scribbled the following:
    | >> Hi every C one,
    | >
    | >> Which key entry (if there is any) should stop this loop?
    | >
    | >> #include <stdio.h>
    | >> int main(){
    | >> int c, getch(void);
    | >
    | >> while((c = getch()) != EOF){
    | >> printf("%c\n", c);
    | >> }
    | >> return 0;
    | >> }
    | >
    | > Well, to be pedantic about it, we don't know. getch() is not defined by
    | > the ISO C standard, and thus it might return anything it wants. There
    | > might be no way for it to ever return EOF. On the other hand, it could
    | > return EOF after every keypress.
    | > To be less pedantic, suppose getch() works the same way as getchar().
    | > Then the answer is "it depends on your operating system". For example,
    | > UNIX uses ^D, Windows uses ^Z, and AmigaOS uses ^\.
    |
    | In fact, there's a function getch() defined as part of the curses
    | package available on Unix-like systems (and probably others). The man
    | page (at least on one system I just checked) makes no mention of EOF.
    | In fact, it's likely to be used in some kind of raw mode in which it's
    | not possible to specify EOF (control-D probably comes through as a
    | literal control-D character, '\004').
    |
    | I think Windows also defines a getch() function that does something
    | different. I have no idea whether it ever returns EOF.
    |
    | To the OP: The fact that the getch() function had to be declared
    | within the program should have been a clue that it's not declared in
    | <stdio.h>, and that it's non-standard.
    |
    | If you want to write portable code, use fgetc(), getc(), or getchar()
    | (your system's documentation should tell you how they differ). If
    | you're curious about getch(), that's a system-specific question; if
    | you can't find answers in your system's documentation, try a
    | system-specific newsgroup.
    |
    | --
    | Keith Thompson (The_Other_Keith)
    <http://www.ghoti.net/~kst>
    | San Diego Supercomputer Center <*>
    <http://users.sdsc.edu/~kst>
    | We must do something. This is something. Therefore, we must do this.
     
    Jean Pierre Daviau, Mar 14, 2005
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.