来自初学者的初步问题 [英] Elementary questions from a beginner

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

问题描述

请原谅我的无知。我在Win32上做过广泛的工作,但是我只需要熟悉C和C ++。最近,我一直在练习来自

K& R.我在评论中写了一些疑问。如果有人能回答,我会感激不尽。$

#include< stdio.h>


void main()

{


/ *此程序会在屏幕上重现您在

键盘上写的内容。 TWICE。

有一次它默认写入输出(在程序之外)和

其他时间写入

因为程序是在工作中。


所以如果你写到屏幕上,它会显示为:


你好,这是一个测试程序

您好,这是一个测试程序


按下回车键即可进行重复。


这是一个我不明白的事情。按回车键,为什么输出重复按

? getchar是否终止

在新行读取?这似乎并非如此,因为在换行后它仍然在while循环内读取
。确实没有
终止循环。它只在每个换行符

后输出设备上的字符。我应该期待的东西

getline()。这是我的疑问。


我通过启发式努力解决的另一个疑问是键盘上的EOF

字符表示是Ctrl + Z.

为什么?


另一个疑问刚刚上升。当我按下Ctrl + C时,它只是

终止而没有警告。当我按下Ctrl + Z时,

询问,按任意键继续。此后按任意键

终止程序。为什么会这样?


我也试过打印EOF。从我看来,

输出的值是-1。 K& R表示EOF值足够大,以便将
与char类型范围区分开来。正如我所见,-1不是真的* BIG

ENOUGH *但是char类型的非法值因为

通常char类型是无符号的。但后来我想起了两个''
补码表示的数字。 K& R

书中是否说它'*足够*因为二进制表示-1

为全1,因此最大可能

范围内的值?


* /


int c;


while((c = getchar())!= EOF)putchar(c);

printf("%d",EOF);

// To模拟EOF,按Ctrl + Z

}

Please forgive my nescience. I have worked extensively on Win32 but I
am only familiar with C and C++. Lately, I have been practicing C from
K&R. Here ''re a few doubts I have written in the comments. I''d be
grateful if someone could answer them.

#include <stdio.h>

void main()
{

/*This program reproduces on the screen whatever you write on your
keyboard. TWICE.
One time it writes the output by default (out of the program) and the
other time it writes
because the program is at work.

So if yo write to the screen the following it appears as:

Hello, this is a test program
Hello, this is a test program

The repitition occurs at the press of the Enter key.

This is one thing I don''t understand. Why is the output repeated at
the press of the enter key? Does getchar terminate
reading at a newline? It doesn''t seem so because after the newline it
is still reading inside the while loop. It does
not terminate the loop. It only puts char on the output device after
every newline. Something I should expect from
getline(). This is my doubt.

Another doubt I have resolved by heuristic endeavour is the EOF
character representation on the keyboard is Ctrl+Z.
Why?

Another doubt has just creeped up. When I press Ctrl+C, it just
terminates without warning. When I press Ctrl+Z, it
asks, "Press any key to continue". Pressing any key thereafter
terminates the program. Why is that so?

I''ve also tried to print EOF. The value, it seems to me from the
output is -1. K&R says EOF value is big enough to
distinguish from char type range. As I see, -1 is not really *BIG
ENOUGH* but an illegal value for a char type because
normally char type is unsigned. But then I am reminded of the two''s
complement representation of numbers. Does the K&R
book say it''s *BIG ENOUGH* because of the binary representation of -1
being all 1s, and therefore the largest possible
value in the range?

*/

int c;

while ((c=getchar()) != EOF) putchar(c);
printf("%d", EOF);
//To emulate EOF, press Ctrl+Z
}

推荐答案

Sathyaish< Vi ********* *******@yahoo.com>潦草地写道:
Sathyaish <Vi****************@yahoo.com> scribbled the following:
请原谅我的无知。我在Win32上做过广泛的工作,但我只熟悉C和C ++。最近,我从K& R开始练习C.我在评论中写了一些疑问。如果有人能回答我,我会很感激。
#include< stdio.h>
void main()


非标准形式的main()。更好的是int main(无效)。

{
/ *此程序在屏幕上重现您在
键盘上写的任何内容。 TWICE。
有一次它默认写入输出(在程序之外)和
其他时间写入
因为程序正在运行。
所以如果哟写入屏幕,则显示如下:
您好,这是一个测试程序
您好,这是一个测试程序
重复发生在新闻界Enter键。
这是我不明白的一件事。为什么在按下回车键的情况下重复输出?


不是。第一行不输出,它是来自你的系统的回声
控制台,C甚至不知道,或关心。第二行是你的程序输出的


getchar是否终止
读取新行?它似乎并非如此,因为在换行后它仍然在while循环中读取。它确实不会终止循环。它只会在每个换行符之后将char放在输出设备上。我应该期待的东西来自getline()。这是我的疑问。


您的控制台(这是一个操作系统概念,而不是C语言)缓冲字符

,直到您按Enter键。然后它将所有这些发送到你的C程序,

,它顺从地将它们显示回到stdout,导致第二行

的文本。

我通过启发式努力解决的另一个疑问是键盘上的EOF
字符表示是Ctrl + Z.
为什么?


因为你的操作系统的制造商认为它会很好。可能是他们想要的任何关键组合



另一个疑问刚刚上升。当我按下Ctrl + C时,它只是在没有警告的情况下终止。当我按下Ctrl + Z时,它会询问,按任意键继续。之后按任意键
终止程序。为什么会这样?


您的程序不会造成这种情况。您的编译环境是。在你的程序中没有代码可以显示按任意键继续,所以它没有b $ b没有做到这一点。您的编译器环境会注意到程序的结束

并打印此按任意键继续一切都靠自己。尝试

退出编译器并直接从命令

行运行程序。

我也尝试打印EOF。从我看来,
输出的值是-1。 K& R表示EOF值足以与char类型范围区分开来。正如我所见,-1不是真的* BIG
ENOUGH *但是char类型的非法值因为
通常char类型是无符号的。但后来我想起了数字的两个补码表示。 K& R
书中是否说它'大*足够*,因为-1
的二进制表示为全1,因此该范围内的最大可能值?


EOF应该是一个通常在(签名)字符范围之外的值。

C标准规定EOF必须是负值。因为

这个,getchar()的返回值是int,而不是char,你应该在你的程序中记下这个。

二进制表示-1不是标准化的。它可能都是1,

但是它可能会有所不同。因此,对于
K& R的评论是错误的,或至少令人困惑。关键是EOF的价值

必须在签名字符范围之外。

* /
int c;
while((c = getchar())!= EOF)putchar(c);
printf("%d",EOF);

//模拟EOF ,按Ctrl + Z


}
Please forgive my nescience. I have worked extensively on Win32 but I
am only familiar with C and C++. Lately, I have been practicing C from
K&R. Here ''re a few doubts I have written in the comments. I''d be
grateful if someone could answer them. #include <stdio.h> void main()
Non-standard form of main(). Better would be int main(void).
{ /*This program reproduces on the screen whatever you write on your
keyboard. TWICE.
One time it writes the output by default (out of the program) and the
other time it writes
because the program is at work. So if yo write to the screen the following it appears as: Hello, this is a test program
Hello, this is a test program The repitition occurs at the press of the Enter key. This is one thing I don''t understand. Why is the output repeated at
the press of the enter key?
It isn''t. The first line isn''t output, it''s echo from your system
console, which C doesn''t even know, or care about. The second line is
output from your program.
Does getchar terminate
reading at a newline? It doesn''t seem so because after the newline it
is still reading inside the while loop. It does
not terminate the loop. It only puts char on the output device after
every newline. Something I should expect from
getline(). This is my doubt.
Your console (which is an OS concept and not a C one) buffers characters
until you press Enter. It then sends all of them to your C program,
which obediently displays them back to stdout, causing the second line
of text.
Another doubt I have resolved by heuristic endeavour is the EOF
character representation on the keyboard is Ctrl+Z.
Why?
Because the makers of your OS thought it would be nice. It could be
any key combination they want.
Another doubt has just creeped up. When I press Ctrl+C, it just
terminates without warning. When I press Ctrl+Z, it
asks, "Press any key to continue". Pressing any key thereafter
terminates the program. Why is that so?
Your program is not causing this. Your compiler environment is. There is
no code in your program to display "Press any key to continue", so it
isn''t doing it. Your compiler environment is noticing the program''s end
and printing this "Press any key to continue" all on its own. Try
exiting your compiler and running your program straight from the command
line.
I''ve also tried to print EOF. The value, it seems to me from the
output is -1. K&R says EOF value is big enough to
distinguish from char type range. As I see, -1 is not really *BIG
ENOUGH* but an illegal value for a char type because
normally char type is unsigned. But then I am reminded of the two''s
complement representation of numbers. Does the K&R
book say it''s *BIG ENOUGH* because of the binary representation of -1
being all 1s, and therefore the largest possible
value in the range?
EOF should be a value normally outside the range of (signed) char.
The C standard specifies EOF has to be a negative value. Because of
this, the return value of getchar() is int, not char, and you should
make note of this in your program.
The binary representation of -1 is not standardised. It could be all 1s,
but then it could be something different. So therefore the comment in
K&R is wrong, or at least confusing. The point is that EOF''s value
must be outside the range of signed char.
*/ int c; while ((c=getchar()) != EOF) putchar(c);
printf("%d", EOF);
//To emulate EOF, press Ctrl+Z
}




-

/ - Joona Palaste( pa*****@cc.helsinki.fi)-------------芬兰-------- \

\-- < a rel =nofollowhref =http://www.helsinki.fi/~palastetarget =_ blank> http://www.helsinki.fi/~palaste ------ ---------------规则! -------- /

"''可以很容易地证明'''的意思''我看过一次这样的证明(我没有这样做)
明白)我不能再记得了''。

- 数学老师



--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''It can be easily shown that'' means ''I saw a proof of this once (which I didn''t
understand) which I can no longer remember''."
- A maths teacher


" Joona I Palaste" < PA ***** @ cc.helsinki.fi>在消息中写道

news:c1 ********** @ oravannahka.helsinki.fi ...
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c1**********@oravannahka.helsinki.fi...
EOF应该是一个通常在范围之外的值(签名)char。
C标准规定EOF必须是负值。
EOF should be a value normally outside the range of (signed) char.
The C standard specifies EOF has to be a negative value.




这将使它至少 (阅读:比零更远)-127。

如果CHAR_BIT> 8.


据我理解的文件,所需要的只是

EOF的类型为int且为负数。 ITYM unsigned char。



That would make it "at least" (read: farther from zero than) -127.
More if CHAR_BIT > 8.

As far as I understand the document, all that is required is that
EOF has the type int and is negative. ITYM unsigned char.


Peter Pichler< pi ***** @ pobox.sk>潦草地写道:
Peter Pichler <pi*****@pobox.sk> scribbled the following:
" Joona I Palaste" < PA ***** @ cc.helsinki.fi>在消息中写道
新闻:c1 ********** @ oravannahka.helsinki.fi ...
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:c1**********@oravannahka.helsinki.fi...
EOF应该是一个通常在(签名)范围之外的值char。
C标准规定EOF必须是负值。
EOF should be a value normally outside the range of (signed) char.
The C standard specifies EOF has to be a negative value.


这将使它至少 (阅读:比零更远)-127。
更多如果CHAR_BIT> 8.
据我理解的文件,所需要的只是
EOF的类型为int且为负数。 ITYM unsigned char。

That would make it "at least" (read: farther from zero than) -127.
More if CHAR_BIT > 8. As far as I understand the document, all that is required is that
EOF has the type int and is negative. ITYM unsigned char.




你认为没错。我在想unsigned char并写下签署

char。称之为大脑与指尖之间的沟通失败。


-

/ - Joona Palaste(pa*****@cc.helsinki。 fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ----------------- ----规则! -------- /

这不对。这甚至都不对。

- Wolfgang Pauli



You think right. I was thinking "unsigned char" and writing "signed
char". Call it a brain-to-fingertips communication failure.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn''t right. This isn''t even wrong."
- Wolfgang Pauli


这篇关于来自初学者的初步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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