getchar函数和EOF问题.. [英] getchar function and EOF problem..

查看:85
本文介绍了getchar函数和EOF问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


有人可以帮我解决这个问题吗?


以下程序应该读取字符直到它符合EOF( -1)

在我的电脑上。

我正在运行linux并使用gcc编译器版本3.4.5。

但它没有't。

我在这里做错了吗?


================== ============================

#include< stdio.h>


int main(无效)

{

char c;


printf(" ; EOF有价值:%d \ n",EOF);


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

putchar( c);


返回0;

}


=========== ====================================

Hello,

Can someone help me out with this one?

The following program should read characters till it met the EOF (-1)
on my pc.
I''m running linux and using the gcc compiler version 3.4.5.
But it doesn''t.
Am I doing something wrong here?.

==============================================

#include <stdio.h>

int main(void)
{
char c;

printf("EOF has value: %d\n", EOF);

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

return 0;
}

===============================================

推荐答案

" broeisi <峰; br ******* @ gmail.com>写道:
"broeisi" <br*******@gmail.com> writes:
以下程序应该读取字符,直到它在我的电脑上遇到EOF(-1)

我正在运行linux并使用gcc编译器版本3.4.5。
但它没有。
我在这里做错了吗?。
The following program should read characters till it met the EOF (-1)
on my pc.
I''m running linux and using the gcc compiler version 3.4.5.
But it doesn''t.
Am I doing something wrong here?.




是的。您没有阅读常见问题解答,它直接回答您的问题




12.1:此代码有什么问题?


char c;

while((c = getchar())!= EOF)...


答:一件事,保持getchar'的返回值的变量必须是/ b $ b是一个int。 getchar()可以返回所有可能的字符值,

以及EOF。通过将getchar的返回值压缩到

char中,正常字符可能会被误解为EOF,

或EOF可能会被更改(特别是如果类型为char,则

未签名)所以从未见过。


参考文献:K& R1 Sec。 1.5 p。 14; K& R2 Sec。 1.5.1 p。 16; ISO

秒6.1.2.5,Sec。 7.9.1,Sec。 7.9.7.5; H& S Sec。 5.1.3 p。 116,

秒15.1,Sec。 15.6; CT& P Sec。 5.1 p。 70; PCS Sec。 11 p。 157.


-

我知道的lusers是如此无能为力,如果他们被扼杀在线索

麝香并且在一堆角质线索的中间掉线,在线索欢迎时间里,他们仍然无法获得线索。

- Michael Girdwood,在修道院中



Yes. You failed to read the FAQ, which answers your question
directly:

12.1: What''s wrong with this code?

char c;
while((c = getchar()) != EOF) ...

A: For one thing, the variable to hold getchar''s return value must
be an int. getchar() can return all possible character values,
as well as EOF. By squeezing getchar''s return value into a
char, either a normal character might be misinterpreted as EOF,
or the EOF might be altered (particularly if type char is
unsigned) and so never seen.

References: K&R1 Sec. 1.5 p. 14; K&R2 Sec. 1.5.1 p. 16; ISO
Sec. 6.1.2.5, Sec. 7.9.1, Sec. 7.9.7.5; H&S Sec. 5.1.3 p. 116,
Sec. 15.1, Sec. 15.6; CT&P Sec. 5.1 p. 70; PCS Sec. 11 p. 157.

--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn''t get a clue."
--Michael Girdwood, in the monastery


" broeisi" <峰; br ******* @ gmail.com>在新闻中写道:1142023302.474499.156390

@ u72g2000cwu.googlegroups.com:
"broeisi" <br*******@gmail.com> wrote in news:1142023302.474499.156390
@u72g2000cwu.googlegroups.com:
================ ==============================

#include< stdio.h>

int main(void)
{
char c;

printf(EOF有值:%d \ n,EOF);

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

返回0;
}
==============================================

#include <stdio.h>

int main(void)
{
char c;

printf("EOF has value: %d\n", EOF);

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

return 0;
}


http:// www.eskimo.com/~scs/c-faq.com.../getcharc.html

-

A. Sinan Unur< 1u **@llenroc.ude.invalid>

(删除.invalid并反转每个组件的电子邮件地址)



http://www.eskimo.com/~scs/c-faq.com.../getcharc.html
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)


我改变了变量char to int。

但我仍有同样的问题。


这是代码:


#包括< stdio.h>


int main(无效)

{

int c;


printf(EOF有价值:%d \ n,EOF) ;(b = getchar())!= EOF)

putchar(c);


返回0;

}

Well I changed the variable from char to int.
But still I have the same problem.

Here is the code:

#include <stdio.h>

int main(void)
{
int c;

printf("EOF has value: %d\n", EOF);

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

return 0;
}


这篇关于getchar函数和EOF问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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