sizeof(int)== 1的实现能否“完全符合"? [英] Can an implementation that has sizeof (int) == 1 "fully conform"?

查看:44
本文介绍了sizeof(int)== 1的实现能否“完全符合"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 C标准 fgetc 以 unsigned char 值的形式返回,转换为 int "(该引用来自C标准,表明确实存在转换)./p>

sizeof(int)== 1 时,许多 unsigned char 值超出范围.因此,某些 unsigned char 值有可能最终被转换为 int 值(转换结果为),尽管文件实际上没有处于错误或文件结束状态.

我惊讶地发现这样的实现实际上存在.TMS320C55x CCS手册文档 UCHAR_MAX 具有相应的值为65535,具有32767的 INT_MAX ,支持二进制模式的 fputs fopen ...更令人惊讶的是,它似乎将环境描述为完全符合标准,完整的实施(减去信号).

C55x C/C ++编译器完全符合ISO规范定义的ISO C标准...

编译器工具带有完整的运行时库.所有图书馆函数符合ISO C库标准....

这样的实现是否可以返回一个值,该值指示没有错误的错误,确实完全符合要求?在循环的条件部分中使用 feof ferror 可以证明这一点是合理的吗(看起来如此可怕)?例如, while((c = fgetc(stdin))!= EOF ||!(feof(stdin)|| ferror(stdin))){...}

解决方案

函数 fgetc()返回 int 值,该值在 unsigned char 仅当读取了正确的字符时,否则返回 EOF ,它是类型为 int 的负值.

我最初的答案(我更改了它)是假设存在对 int 的整数转换,但事实并非如此,因为实际上函数 fgetc()是已经返回了 int 类型的值.

我认为,要符合标准,该实现必须使 fgetc()返回 int 范围内的非负值,除非 EOF .

这样,从32768到65535的值范围将永远不会与TMS320C55x实现中的字符代码相关联.

According to the C standard, any characters returned by fgetc are returned in the form of unsigned char values, "converted to an int" (that quote comes from the C standard, stating that there is indeed a conversion).

When sizeof (int) == 1, many unsigned char values are outside of range. It is thus possible that some of those unsigned char values might end up being converted to an int value (the result of the conversion being "implementation-defined or an implementation-defined signal is raised") of EOF, which would be returned despite the file not actually being in an erroneous or end-of-file state.

I was surprised to find that such an implementation actually exists. The TMS320C55x CCS manual documents UCHAR_MAX having a corresponding value of 65535, INT_MAX having 32767, fputs and fopen supporting binary mode... What's even more surprising is that it seems to describe the environment as a fully conforming, complete implementation (minus signals).

The C55x C/C++ compiler fully conforms to the ISO C standard as defined by the ISO specification ...

The compiler tools come with a complete runtime library. All library functions conform to the ISO C library standard. ...

Is such an implementation that can return a value indicating errors where there are none, really fully conforming? Could this justify using feof and ferror in the condition section of a loop (as hideous as that seems)? For example, while ((c = fgetc(stdin)) != EOF || !(feof(stdin) || ferror(stdin))) { ... }

解决方案

The function fgetc() returns an int value in the range of unsigned char only when a proper character is read, otherwise it returns EOF which is a negative value of type int.

My original answer (I changed it) assumed that there was an integer conversion to int, but this is not the case, since actually the function fgetc() is already returning a value of type int.

I think that, to be conforming, the implementation have to make fgetc() to return nonnegative values in the range of int, unless EOF is returned.

In this way, the range of values from 32768 to 65535 will be never associated to character codes in the TMS320C55x implementation.

这篇关于sizeof(int)== 1的实现能否“完全符合"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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