是EOF总是消极的? [英] Is EOF always negative?

查看:147
本文介绍了是EOF总是消极的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时的EOF总是消极的?

我想编写一个函数,在输入读取下一个单词并返回字或EOF被发现,如果输入的结尾已经达到了行号。如果EOF不一定是负面的,作用是不正确的。


解决方案

是的,EOF总是负的。

标准说:


  

7.19输入/输出

  7.19.1简介


  
  

3的宏[...]这EOF
  扩展为一个整型常量
  前pression,int型的和
  负值,由返回
  几个功能指示
  结束文件,也就是,没有更多的输入
  从流;


请注意,有与普通字符签字没有问题。在< stdio.h中> 这对付字符职能,特别是投的字符 unsigned char型再到 INT ,让所有有效字符有积极的价值。例如:

  INT龟etc(FILE *流)


  

7.19.7.1

  ...在龟etc功能获得该字符为unsigned char转换为int ...


Is EOF always negative?

I'm thinking of writing a function that reads the next word in the input and returns the line number the word was found in or EOF if the end of the input has been reached. If EOF is not necessarily negative, the function would be incorrect.

解决方案

Yes, EOF is always negative.

The Standard says:

7.19 Input/output
7.19.1 Introduction

3 The macros are [...] EOF which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;

Note that there's no problem with "plain" char being signed. The <stdio.h> functions which deal with chars, specifically cast the characters to unsigned char and then to int, so that all valid characters have a positive value. For example:

int fgetc(FILE *stream)

7.19.7.1
... the fgetc function obtains that character as an unsigned char converted to an int ...

这篇关于是EOF总是消极的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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