无法理解人物处理程序 [英] cannot understand the character handling Program

查看:50
本文介绍了无法理解人物处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用Dennis Ritchie的C编程语言

执行程序时,我尝试运行以下程序。我正在使用

Dev ++作为编译软件。

计划如下所示。

#include< stdio.h>

main()

{

long nc;


nc = 0;

while(getchar()!= EOF)

++ nc;

printf("%1d \ n",nc);

}


无论如何我正在打字,它显示在黑色的窗口中。即使

给出了相当大的投入,我也不能按照声明中的规定达到EOF。我有两个问题。


第一个是我无法理解程序的逻辑

即使经过它。

无论我们输入什么输入都出现在窗口中。

是否有存储数据的地方。如果有人能用外行的语言解释这个程序的用法,我会感激不尽。


第二个问题是,我们正在使用EOF来将它与

值进行比较。在我们在

比较中使用它之前是否需要指定EOF值?我能想到的一种方法是EOF值可能是变量可以容纳的最大值(在此长中)。但我是

不确定。


请帮我理解。

解决方案



Gladiator写道:

当我尝试从C编程语言执行程序时
Dennis Ritchie,我试图运行以下程序。我使用的是Dev ++作为编译器软件。
程序如下所示。

#include< stdio.h>
main()
{
long nc;

nc = 0;
while(getchar()!= EOF)
++ nc;
printf("%1d \ n",nc);
无论我输入什么,它都会显示在黑色窗口中。即使在给出相当大的投入之后,我也无法按照声明中的规定到达EOF。我有2个问题。

首先了解程序。

这是从标准输入中获取的输入(默认情况下是键盘 - getchar

是否可以)。计算输入的字符数,并在输入结束时显示计数

(在标准输出 - 终端中)。输入结束是

EOF(文件结束)。我不确定DOS但是在unix控件+ d中给出了EOF


第一是我无法理解程序的逻辑
即使经过它。
无论我们输入什么输入都出现在窗口中。是否有存储数据的地方。如果有人能用外行的语言来解释这个程序的用法,我将感激不尽。

第二个问题是,我们正在使用EOF将其与
进行比较值。在我们使用它之前是否有必要指定EOF值?
比较?我能想到的一种方式是EOF值可以是变量可以容纳的最大值(在此长中)。但是我不确定它。

EOF不是由任何大小定义的任何最大值。它表明

thatere不再有可用的输入。它在stdio中定义。 h和

unix它是ctrl + d。

EOF出现在所有文件的末尾(至少在unix中)


如果ctrl + d在你的操作系统中没有用于EOF请参考操作系统手册

请帮助我理解它。




Gladiator写道:

#include< stdio.h>
main()
{n / n}

nc = 0;
while(getchar()!= EOF)
++ nc;
printf("%1d \ n",nc) ;
}

无论我输入什么,它都显示在黑色窗口中。即使在给出相当大的投入之后,我也无法按照声明中的规定到达EOF。


只要可以从

输入流stdin中获取另一个字符,程序就会循环。如果您使用键盘输入(在没有来自文件或其他程序的
管道),它将继续接受字符

,直到您以某种方式终止输入流。终止是OS

特定的,通常是一个特殊的键或键序列。

1st是我无法理解程序的逻辑
即使经过它。
我们输入的输入是否出现在窗口中。是否有存储数据的地方。


不是你的计划。

如果有人能用外行的语言解释这个程序的用法,我会很感激。


getchar()从标准输入流中读取下一个输入字符。

如果没有其他字符,则返回EOF(对于文件结尾)

而不是一个角色。每次返回值不是EOF时,nc是

递增。


当没有更多字符时,循环终止并且printf

语句应该打印读取的字符数。你的程序使用

不正确的格式,%1d,应该是%ld,因为变量nc

是一个长整数。

2nd问题是,我们使用EOF将其与
值进行比较。在我们使用它之前是否有必要指定EOF值?
比较?


是的。幸运的是,已经完成的是< stdio.h>,在使用EOF之前你包含了



我能想到的一种方式是EOF值可能是
变量可以容纳的最大值(在此长中)。但是我不确定它。




EOF是一个整数值,它与所有输入字符值分开

转换到unsigned int(我们在这里处理简单类型char

字符,而不是宽字符)。通常它是-1,但它可能从一个实现到另一个实现不同。


-

Thad


" M.B" < MB ******* @ gmail.com>写道:

Gladiator写道:



[snip]

第二个问题是,我们是使用EOF将其与
值进行比较。在我们使用它之前是否有必要指定EOF值?
比较?我能想到的一种方式是EOF值可以是变量可以容纳的最大值(在此长中)。但是我不确定它。


EOF不是由任何大小定义的任何最大值。它表明
thatere不再有可用的输入。它在stdio中定义。 h和
unix它是ctrl + d。
EOF出现在所有文件的末尾(至少在unix中)

如果ctrl + d在你的操作系统中不起作用EOF请参考操作系统手册




你们两个都误解了EOF的全部内容。


每个文件(包括stdin)有一个文件结束指示符。这不是一个字符或一个价值的b $ b,这是一个条件;它表示最后的

尝试从文件中读取失败,因为它已经到达文件的结尾




EOF是< stdio.h>中定义的宏。它指定某些输入函数(例如getchar())在

结束文件或错误条件下返回的值。除了它是一个负整数(它通常为-1)之外,EOF的值不是由

指定的。

getchar()函数尝试读取字符。如果

成功,则返回该字符的值,作为无符号

char转换为int。 (在一个典型的系统中,这将是一个值,在
范围内为0..255。)如果失败,则返回值EOF,即

不同于任何有效的字符值。


系统通常会有一些机制让用户在程序读取时产生

文件结束条件一个交互式的

设备,如键盘。在Unix下,这通常由

键入control-D表示。在MS-DOS下,它通常是控制-Z。你的程序

不会看到control-D(4)或control-Z(26)值;它将改为

看到值EOF。


磁盘文件可能会或可能没有一些标记在它的末尾

表示文件的结尾。在MS-DOS下,文本文件最后可能有一个

control-Z字符;在Unix下,系统只需跟踪文件的大小。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


When I am trying to execute a program from "The C Programming Language"
by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.
#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am not
able to reach the EOF as specified in the While statement even after
giving a considerable inputs. I have 2 questions.

1st is I simply cannot understand the logic of the program
even after going through it.
Whatever the input we are typing in is appearing in the window. Is
there any where the data is being stored. I would be thankful if
someone could explain the use of the program in layman''s language.

2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?. One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.

Please help me to understand it.

解决方案


Gladiator wrote:

When I am trying to execute a program from "The C Programming Language"
by Dennis Ritchie, I tried to run the following program.I am using
Dev++ as a compiler software. The
Program is presented below.
#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am not
able to reach the EOF as specified in the While statement even after
giving a considerable inputs. I have 2 questions.
first of all understand the program.
this takes input from standard input (keyboard by default - getchar
does it) . count the number of characters typed and displays the count
on end of input (in standard output - terminal). the end of input is
EOF (end of file). I am not sure of DOS but in unix control+d gives EOF

1st is I simply cannot understand the logic of the program
even after going through it.
Whatever the input we are typing in is appearing in the window. Is
there any where the data is being stored. I would be thankful if
someone could explain the use of the program in layman''s language.

2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?. One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.
EOF is not any maximum defined by size of anything. it indicated that
thatere are no more input available. it is defined in stdio . h and in
unix it is ctrl+d.
EOF is present at the end of all files (atleast in unix)

if ctrl+d dont work in ur OS for EOF pls refer to OS manual for it
Please help me to understand it.




Gladiator wrote:

#include <stdio.h>
main()
{
long nc;

nc = 0;
while(getchar() != EOF)
++nc;
printf("%1d\n",nc);
}

Whatever I am typing, It is displayed in the black window. I am not
able to reach the EOF as specified in the While statement even after
giving a considerable inputs.
The program loops as long as another character can be obtained from the
input stream stdin. If you are using keyboard input (in the absence of
piping from a file or other program), it continues to accept characters
until you somehow terminate the input stream. The termination is OS
specific, usually a special key or key sequence.
1st is I simply cannot understand the logic of the program
even after going through it.
Whatever the input we are typing in is appearing in the window. Is
there any where the data is being stored.
Not by your program.
I would be thankful if
someone could explain the use of the program in layman''s language.
getchar() reads the next input character from the standard input stream.
If there are no more characters, EOF (for End of File) is returned
instead of a character. Each time the return value is not EOF, nc is
incremented.

When there no more characters, the loop terminates and the printf
statement should print the number of characters read. Your program uses
an incorrect format, %1d, which should be %ld, because the variable nc
is a long int.
2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?.
Yes. Fortunately, that is already done is <stdio.h>, which you included
prior to using EOF.
One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.



EOF is an integer value that is separate from all input character values
converted to an unsigned int (we are handling simple type char
characters here, not wide characters). Typically it is -1, but it may
vary from one implementation to another.

--
Thad


"M.B" <mb*******@gmail.com> writes:

Gladiator wrote:


[snip]

2nd question is, we are using the EOF to compare it with a
value. Is it necessary to specify the EOF value before we use it in a
comparison?. One way what i could think is the EOF value may be the
maximum value that the variable can hold ( In this "Long"). But I am
not sure about it.


EOF is not any maximum defined by size of anything. it indicated that
thatere are no more input available. it is defined in stdio . h and in
unix it is ctrl+d.
EOF is present at the end of all files (atleast in unix)

if ctrl+d dont work in ur OS for EOF pls refer to OS manual for it



Both of you have misunderstood what EOF is all about.

Each file (including stdin) has an end-of-file indicator. This isn''t
a character or a value, it''s a condition; it indicates that the last
attempt to read from the file failed because it had reached the end of
the file.

EOF is a macro defined in <stdio.h>. It specifies the value that will
be returned by certain input functions, such as getchar(), on an
end-of-file or error condition. The value of EOF isn''t specified by
the standard except that it''s a negative integer (it''s typically -1).
The getchar() function attempts to read a character. If it was
successful, it returns the value of that character, as an unsigned
char converted to int. (On a typical system, this will be a value in
the range 0..255.) If it failed, it returns the value EOF, which is
distinct from any valid character value.

A system will typically have some mechanism for the user to cause an
end-of-file condition when a program is reading from an interactive
device such as a keyboard. Under Unix, this is typically indicated by
typing control-D. Under MS-DOS, it''s usually control-Z. Your program
won''t see the control-D (4) or control-Z (26) value; it will instead
see the value EOF.

A disk file may or may not have some marker at the end of it to
indicate the end of the file. Under MS-DOS, a text file might have a
control-Z character at the end; under Unix, the system simply keeps
track of how big the file is.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.


这篇关于无法理解人物处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆