为什么'fputc'使用INT作为参数而不是CHAR? [英] Why 'fputc' use an INT as its parameter instead of CHAR?

查看:647
本文介绍了为什么'fputc'使用INT作为参数而不是CHAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准C lib:

  int fputc(int c,FILE * stream); 

此类行为多次发生,例如:

  int putc(int c,FILE * stream); 
int putchar(int c);

为什么不使用CHAR?
如果使用INT是必要的,那么我应该什么时候使用INT而不是CHAR?

解决方案

/ em>(在我看来,因为早期C背后的大部分基本原理都是在时间的深处丢失的),它只是简单地将> fgetc 类型函数必须能够返回任何真正的字符 EOF 特殊字符。 fgetc 函数将下一个字符转换为 int ,并使用特殊标记值 EOF 来指示流的结束。



为此,他们需要更广泛的 int type,因为 char 不足以容纳所有可能的字符加上一件事。



而且,由于C的开发人员似乎更喜欢使用相当简单的方法来编写代码,所以它们使用相同的类型是合理的,以允许代码如下所示:

  filecopy(ifp,ofp)
FILE * ifp;
FILE * ofp;
{
int c; ((c = fgetc(ifp))!= EOF)
fputc(c,ofp);
}


standard C lib:

int fputc(int c , FILE *stream);

And such behaviors occured many times, e.g:

    int putc(int c, FILE *stream);
    int putchar(int c);

why not use CHAR as it really is? If use INT is necessary, when should I use INT instead of CHAR?

解决方案

Most likely (in my opinion, since much of the rationale behind early C is lost in the depths of time), it it was simply to mirror the types used in the fgetc type functions which must be able to return any real character plus the EOF special character. The fgetc function gets the next character converted to an int, and uses a special marker value EOF to indicate the end of the stream.

To do that, they needed the wider int type since a char isn't quite large enough to hold all possible characters plus one more thing.

And, since the developers of C seemed to prefer a rather minimalist approach to code, it makes sense that they would use the same type, to allow for code such as:

filecopy(ifp, ofp)
    FILE *ifp;
    FILE *ofp;
{
    int c;
    while ((c = fgetc (ifp)) != EOF)
        fputc (c, ofp);
}

这篇关于为什么'fputc'使用INT作为参数而不是CHAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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