问题描述
下面是我编写的代码,用于计算输入的字符数.I
假设EOF为-1,所以如果我输入-1然后按回车不应该
程序结束?如果我在while循环中输入类似''q'的东西,那么它就会结束循环。
是什么东西?
< code>
#include< stdio.h>
void main(){
long nc;
nc = 0;
while(getchar()!=''EOF'') {
++ nc;
}
printf("%ld \ n",nc);
}
< / code>
谢谢
Dave
Below is the code ive written just to count the characters typed in. I
assumed EOF is -1, so if i type -1 and then press enter shouldnt the
program end? It orks if i put something like ''q'' in the while loop to
end the loop.
what is up?
<code>
#include <stdio.h>
void main() {
long nc;
nc = 0;
while (getchar() != ''EOF'') {
++nc;
}
printf("%ld\n", nc);
}
</code>
thanks
Dave
推荐答案
不,因为当你输入-1时,它不会被存储为一个角色,而是两个
个字符'' - ''和''1'。 -1然而是一个单个字符,而那两个
值并不匹配。
但是,你可以自己做一个测试,并检查一系列字母。
你应该很容易做到。我相信理论是将所有
输入字符放入一个数组中,并检查
数组中的最后两个字符是否等于'' - ''和''1'。
-
Tim Cambrant
< tim at cambrant dot com>
No, because when you type -1 it isn''t stored as a character, but two
characters ''-'' and ''1''. ''-1'' however is a single character, and those two
values doesn''t match.
However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to ''-'' and ''1''.
--
Tim Cambrant
<tim at cambrant dot com>
不,因为当你输入时 - 1它不是作为一个角色存储的,而是两个
字符 - 和1。 ''-1''然而是一个单个字符,而且这两个值不匹配。
然而,你可以自己做一个测试,并检查一系列字母。
你应该很容易做到。我相信理论是将所有
输入字符放入一个数组中,并检查
数组中的最后两个字符是否等于'' - ''和''1'。
No, because when you type -1 it isn''t stored as a character, but two
characters ''-'' and ''1''. ''-1'' however is a single character, and those two
values doesn''t match.
However, you could do a test yourself, and check for a series of letters.
Should be quite easy for you to do. I believe the theory is to put all the
input characters into an array, and check if the last two characters in the
array is equal to ''-'' and ''1''.
或者更好的是,如果您的平台支持它,请输入用于表示输入数据结束信号的密钥或密钥组合
。 />
在基于MSDOS的系统上,这将是^ Z键组合
在基于Unix的系统上,这将是组合产生''eof'的原因'
信号(通常是^ D,但请检查你的stty设置以确定)。
-
Lew Pitcher
企业技术解决方案IT顾问
多伦多道明银行金融集团
(表达的意见是我自己的,而不是我的雇主'') >
Or better yet, if your platform supports it, enter the key or key combination
that signals end-of-input-data.
On MSDOS-based systems, this would be the ^Z key combination
On Unix-based systems, this would be what ever combination generates the ''eof''
signal (typically ^D, but check your stty settings to be sure).
--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group
(Opinions expressed are my own, not my employers'')
问候
Irrwahn
-
闭上眼睛,按三次逃生。
Regards
Irrwahn
--
Close your eyes and press escape three times.
这篇关于基本的c / o和EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!