为什么模量(%)运算符的工作对于char而不是浮点类型? [英] Why does modulus (%) operator work for char but not for floating types?
问题描述
#include <stdio.h>
int main()
{
char c;
c=10;
if(c%2==0)
printf("Yes");
return 0;
}
以上code打印是。谁能告诉为什么模运算符适用于字符
和 INT
而不是双击
等。
推荐答案
您已经有了意见,解释为什么%
是字符$定义C $ C>:这是所有整数类型的界定,C,
字符
是一个整数类型。一些其他的语言作定义,不支持算术运算鲜明的字符
类型,但C是不是其中之一。
You already got comments explaining why %
is defined for char
: it's defined for all integer types, and in C, char
is an integer type. Some other languages do define a distinct char
type that does not support arithmetic operations, but C is not one of them.
但是,为了回答为什么它不为浮点类型定义:历史。没有任何技术原因为什么不可能定义%
运营商浮点类型。这里的C99理由这样说:
But to answer why it isn't defined for floating-point types: history. There is no technical reason why it wouldn't be possible to define the %
operator for floating-point types. Here's what the C99 rationale says:
6.5.5乘法运算符
[...]
在C89委员会拒绝延长%
运营商浮动类型,这种用法将重复由 FMOD $ C $提供的设施工作C>(见§7.12.10.1)。
The C89 Committee rejected extending the %
operator to work on floating types as such usage would duplicate the facility provided by fmod
(see §7.12.10.1).
和作为mafso后来发现:
And as mafso found later:
7.12.10.1的FMOD功能
[...]
在C89委员会审议使用求余运算符%
此功能的建议;但被拒绝,因为一般的运营商对应的硬件设施和 FMOD
硬件不支持大多数机器。
The C89 Committee considered a proposal to use the remainder operator %
for this function; but it was rejected because the operators in general correspond to hardware facilities, and fmod
is not supported in hardware on most machines.
他们似乎有点矛盾。在%
运营商并没有延长,因为 FMOD
已经填补了需求,但 FMOD
被挑来填补这一需求,因为委员会不希望延长%
运营商?他们不能很好地同时为真同时
They seem somewhat contradictory. The %
operator was not extended because fmod
already filled that need, but fmod
was picked to fill that need because the committee did not want to extend the %
operator? They cannot very well both be true at the same time.
我怀疑这些原因之一是原来的原因,另一个是不晚重新访问该决定的原因,但没有告诉这是第一次。无论哪种方式,它只是决定%
不会执行此操作。
I suspect one of these reasons was the original reason, and the other was the reason for not later re-visiting that decision, but there's no telling which was first. Either way, it was simply decided that %
wouldn't perform this operation.
这篇关于为什么模量(%)运算符的工作对于char而不是浮点类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!