为什么'\\ 97'的ASCII值等于55 [英] why '\97' ascii value equals 55

查看:294
本文介绍了为什么'\\ 97'的ASCII值等于55的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像 C 的code:

#include<stdio.h>

int main(void) {
      char c = '\97';
      printf("%d",c);
      return 0;
}

结果是 55 ,但我不知道如何计算。
我知道八进制数或十六进制数遵循'\\',莫非 97 是十六进制数?

the result is 55,but I can't understand how to calculate it. I know the Octal number or hex number follow the '\', does the 97 is hex number?

推荐答案

\\ 是一个八进制转义序列,但 9 不是有效的八进制数字这样反而间$ p $的pting它作为八进制就是做PTED为多字符间$ p $常数a \\ 9 1 ,其价值被定义实现。没有任何警告标志 GCC 默认提供了以下警告:

\ is a octal escape sequence but 9 is not a valid octal digit so instead of interpreting it as octal it is being interpreted as a multi-character constant a \9 and a 1 whose value is implementation defined. Without any warning flags gcc provides the following warnings by default:

warning: unknown escape sequence: '\9' [enabled by default]
warning: multi-character character constant [-Wmultichar]
warning: overflow in implicit constant conversion [-Woverflow]

在节中的C99标准草案 6.4.4.4 字符常量的段落的 10 的说(的重点煤矿的):

The C99 draft standard in section 6.4.4.4 Character constants paragraph 10 says (emphasis mine):

这是整型字符常量的类型为int。整型字符常量的值
  包含映射到单字节执行字符一个字符是
  PTED为整数的映射字符间$ P $重新presentation的数值。
  包含多个字符的整型字符常量的值(例如,
  AB),或者包含未映射到单字节字符或转义序列
  执行字符,是实现定义

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

例如 GCC 实施记录的这里和如下:

For example gcc implementation is documented here and is as follows:

编译器在每次计算一个多字符字符常量的字符,移由每个目标人物的位数离开了previous值,然后或-ING在截断新字符的位模式到的目标字符的宽度。最终位模式给出int类型,并因此被签名,而不管单个字符是否签署与否(从3.1版本的微小变化和更早的GCC)。如果有更多的字符的恒定比将适合在目标的int编译器将发出一个警告,和过量的领先字符被忽略。

The compiler evaluates a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not (a slight change from versions 3.1 and earlier of GCC). If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.

例如,'AB'与8位字符的目标将是PTED为(INT)((unsigned char型)'A'* 256 +(unsigned char型)'B')'之间的$ P $和\\ 234A'为'(INT)((unsigned char型)'\\ 234'* 256 +(无符号字符)'A')。

For example, 'ab' for a target with an 8-bit char would be interpreted as ‘(int) ((unsigned char) 'a' * 256 + (unsigned char) 'b')’, and '\234a' as ‘(int) ((unsigned char) '\234' * 256 + (unsigned char) 'a')’.

据我可以告诉这是被间preTED为:

As far as I can tell this is being interpreted as:

char c = ((unsigned char)'\71')*256 + '7' ;

这将导致 55 ,这与多字符常量的实施上面,虽然翻译 \\一致9 \\ 71 并不明显。

which results in 55, which is consistent with the multi-character constant implementation above although the translation of \9 to \71 is not obvious.

修改

我意识到以后究竟发生什么事是 \\ 正在下降,因此 \\ 9 - &GT; 9 ,所以我们真正拥有的是:

I realized later on what is really happening is the \ is being dropped and so \9 -> 9, so what we really have is:

c = ((unsigned char)'9')*256 + '7' ;

这似乎更合理,但仍武断的,我不清楚为什么这不是一个直出错误。

which seems more reasonable but still arbitrary and not clear to me why this is not a straight out error.

更新

从阅读的附注的C ++参考手册我们发现,在经典C 的和旧版本的C ++,当反斜杠后的字符不是定义为景观序列,它等于于字符的数值。 ARM节 2.5.2

From reading The Annotated C++ Reference Manual we find out that in Classic C and older versions of C++ when backslash followed character was not defined as an scape sequence it was equal to the numeric value of the character. ARM section 2.5.2:

这不同于经典的C和C ++的早期版本,其中blackslash随后在源字符集中的字符序列的值,如果不定义为一个转义序列,就等于跨pretation于字符的数值。例如,'\\ Q'就等于'Q'。

This differs from the interpretation by Classic C and early versions of C++, where the value of a sequence of a blackslash followed by a character in the source character set, if not defined as an escape sequence, was equal to the numeric value of the character. For example '\q' would be equal to 'q'.

这篇关于为什么'\\ 97'的ASCII值等于55的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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