stopping a while loop

J

Jean Pierre Daviau

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;
}
 
J

Joona I Palaste

Jean Pierre Daviau said:
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 ^\.
 
K

Keith Thompson

Joona I Palaste said:
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.
 
J

Jean Pierre Daviau

Thak you.


"Keith Thompson" <[email protected]> a écrit dans le message de
(e-mail address removed)...
| > Jean Pierre Daviau <[email protected]> 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) (e-mail address removed)
<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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top