问题描述
这是一个简单的程序,计算字符串,符号和单词。
Here is a simple program, counting strings, symbols and words.
使用Cygwin可以进行一切计算。
Everything is ok with the computation, using Cygwin.
但是在输入值之后启动程序时不会打印 nc
, nw
, nl
并等待
But while launching, after input values, the program doesn't print nc
, nw
, nl
and wait for entering further values.
将 EOF
更改为13(按Enter)仍然无济于事。
Changing EOF
to 13 (Enter) doesn't help anyway.
+ 也很有用:程序正在停止,写 [n] + Stopped
,其中 n始终是不同的数字。
+ is useful too: the program is stopping, writing [n]+ Stopped
, where 'n' is always different number.
代码为
#include <stdio.h>
#define IN 1
#define OUT 0
int main () {
char c;
int state;
int nc, nw, nl;
state = OUT;
while ((c=getchar()) != EOF) {
nc++;
if (c == 'n')
nw++;
if (c == '\n' || c == ' ' || c == '\t')
state = OUT;
else if (state = OUT){
state = IN;
nw++;
}
}
printf ("%d %d %d", nc, nl, nw);
}
推荐答案
getchar
返回 int
,而不是 char
。之所以必须这样做,是因为它可以返回字符值或返回特殊值 EOF
来指示输入文件的结尾。由于 EOF
必须与 char
类型的所有值都不同,因此它没有类型 char
。因此,您需要将包含其返回值的变量定义为 int
,而不是 char
。
getchar
returns int
, not char
. It has to do this because it can either return a character value or return the special value EOF
to indicate the end of the input file. Since EOF
has to be different from all values of the char
type, it does not have the type char
. So you need to define the variable that contains its return value as an int
, not as char
.
在某些平台上, char
类型是带符号的,而 getchar
函数实际上并不返回字符值,而是将字符值包装为非负值。实际上,这意味着ASCII字符不变,但是在某些平台上,非ASCII字符被转换为128到255之间的值,而不是-128到-1之间的值。因此,通常,在将其用作字符时,需要将 getchar
的返回值转换为 char
。在 char
类型为未签名的平台上,这不会更改任何内容,但是在 char
类型为的平台上
On some platforms, the char
type is signed, and the getchar
function does not in fact return the character value, but the character value wrapped around to a non-negative value. In practice, what this means is that ASCII characters are unchanged but, on some platforms, non-ASCII characters are transformed to a value between 128 and 255 instead of between -128 and -1. So in general you need to cast the return value of getchar
to an char
when you use it as a character. On platforms where the char
type is unsigned, this doesn't change anything, but on platforms where the char
type is signed, it is necessary to avoid character 255 being mistaken as the end of the file.
int c;
…
while ((c = getchar()) != EOF) {
…
if (c == 'n') /* ok because 'n' is part of ASCII and thus always positive */
…
if ((char)c == some_other_char) /*cast needed in general*/
…
}
在Cygwin中,像在其他类似Unix的系统中一样,按 + 在行的开头表示输入的结束。在Windows下,您需要按 + (在Cygwin下,再次类似于其他与Unix类似的系统,它会挂起程序:它已停止,但可以恢复使用命令 bg
)。这些都不向程序发送控制字符,它们都发送文件结尾指示,C的 getchar
实现将转换为 EOF
值。
In Cygwin, like in other Unix-like systems, press + at the beginning of a line to signal the end of the input. Under Windows, you need to press + (under Cygwin, again like in other Unix-like systems, that instead suspends the programs: it's stopped, but can be resumed with the command bg
). Neither of these send a control character to the program, they send an end-of-file indication which C's getchar
implementation translates to the EOF
value.
这篇关于getchar()不会传递EOF,并且Ctrl + Z不会终止Cygwin上的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!