简体   繁体   English

当应用于字符算术时,模运算符是什么意思?

[英]What does the modulo operator mean when applied to character arithmetic?

symbol = (rand() % ('~' - ' ' + 1)) + ' ';

I have understood that every time that i run the program "symbol" is different. 我已经了解到,每次我运行程序“符号”都是不同的。 But , why can't I use 但是,为什么我不能使用

symbol = rand();

There's nothing called character arithmatic Note (AFAIK) and the modulo operator means it's usual business. 没有什么叫做字符算术 注释 (AFAIK),取模运算符意味着这是平常的事。 What you seem to be confused is about the whole expression. 您似乎对整个表达感到困惑。

As per the ASCII table 按照ASCII表

  • ~ has a decimal value of 126 ~的十进制值为126
  • has decimal value of 32. 十进制值为32。

The output of rand() will be modulo-valued by the range, over the space , so as to produce a printable representation . rand()的输出将在整个空间上按范围取模值,以产生可打印的表示形式

To break it down, 要分解,

  • ('~' - ' ' + 1) == 126-32 +1 == 95 ('~' - ' ' + 1) == 126-32 +1 == 95
  • (rand() % ('~' - ' ' + 1)) produces a random number between 0-94 . (rand() % ('~' - ' ' + 1))产生一个介于0-94之间的随机数。
  • (rand() % ('~' - ' ' + 1)) + ' ' yields a random number between 32-126 , which is the printable range. (rand() % ('~' - ' ' + 1)) + ' '产生一个介于32-126之间的随机数,这是可打印范围。

why I can't use symbol = rand(); 为什么我不能使用symbol = rand();

Well, there's nothing wrong in that statement, but looks like the purpose is to produce a printable symbol value. 好的,该语句没有错,但是看起来目的是产生可打印的 symbol值。


Note: 注意:

Quoting C11 , chapter §6.4.4.4 引用C11 ,第§6.4.4.4章

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. 整数字符常量是由单引号引起的一个或多个多字节字符的序列,例如'x'。 [...] [...]

and, 和,

An integer character constant has type int . 整数字符常量的类型为int [...] [...]

so, it's integer arithmetic, all the way. 所以,一直都是整数运算。

Ascii of 的Ascii

  • ~ is 126 ~是126

  • space is 32 space是32

So symbol = (rand() % ('~' - ' ' + 1)) + ' '; 因此symbol = (rand() % ('~' - ' ' + 1)) + ' '; is equivalent to: 等效于:

symbol = (rand() % (126 - 32 + 1)) + 32;

which gives you a random number between 32 and 126 给你一个介于32126之间随机数


C99 7.20.2.1 C99 7.20.2.1

The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX . rand函数计算范围为0到RAND_MAX的伪随机整数序列。

The value of the RAND_MAX macro shall be at least 32767 . RAND_MAX宏的值至少为32767

You can use symbol=rand() if you want a range between 0 and min(max(32767,RAND_MAX),TYPE_SYMBOL_MAX) and as long as the type of symbol permit that . 如果您希望范围介于0min(max(32767,RAND_MAX),TYPE_SYMBOL_MAX)之间 ,并且只要symbol类型允许,则可以使用symbol=rand()

Character constants in C are integers. C中的字符常量是整数。 So the following: 因此,以下内容:

('~' - ' ' + 1)

Calculates the offset of the encoding for '~' from the encoding for ' ' and add one to it. 计算'~'的编码与' '的编码的偏移量,并将其加一。 It's how many different character encodings lie between them, let's call that number m . 它们之间有多少种不同的字符编码,我们将其称为m

(rand() % ('~' - ' ' + 1))

Takes a pseudo-random number and maps it to the range [0, m) , let's call that number n . 取一个伪随机数并将其映射到[0, m)范围,我们将该数字称为n

(rand() % ('~' - ' ' + 1)) + ' '

This simply takes n , treating it as an offset, and adds it to the encoding of ' ' , thus getting an encoding of a symbol. 这简单地采用n ,将其视为偏移量,并将其添加到' '的编码中,从而获得符号的编码。 The symbol will always be one of a certain set. 该符号将始终是某个集合中的一个。


symbol = rand();

The above will convert an integer, that possibly lies way outside the range of char , into some a character value. 上面的代码会将一个可能超出char范围之外的整数转换为某个字符值。 That will be done in some implementation defined manner. 这将以某种实现定义的方式完成。 That's not the most reliable way to get correct results, to say the least. 至少可以说,这不是获得正确结果的最可靠方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM