解释反斜杠用C [英] Explain Backslash in C

查看:198
本文介绍了解释反斜杠用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以请解释一下下面的code和请在这种情况下也解释了反斜线的作用( \\ )。什么 \\ \\ \\ OOO \\ \\ \\ <?/ code>意思?

Can anyone please explain the below code and please also explain the role of backslash( \ ) in such situations. And what \' , \" , \ooo , \ \ , \? means?

#include <stdio.h>
int main(){
    char a = '\010';
    char y = '010';
    printf("%d,%d",a,y);

    return 0;
}

输出:8.48

output: 8,48

推荐答案

'\\ 010八进制转义序列 10 八进制为 8 小数,它会被提升到一个 INT 当调用的printf 这样解释的价值。

This '\010' is a octal escape sequence 10 in octal is 8 in decimal and it will be promoted to an int when calling printf so that explains that value.

010 多字符常量的,它的值是实现定义的,如果我们看一下C99标准草案 6.4.4.4 字符常量的段落的 10 的说(的重点煤矿的):

This '010' is a multi-character constant and it's value is implementation defined, if we look at the C99 draft standard section 6.4.4.4 Character constants paragraph 10 says(emphasis mine):

[...]包含多个字符的整型字符常量的值(例如,
  AB),或者包含未映射到单字节字符或转义序列
  执行字符,是实现定义的。[...]

[...]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 你会看到至少这样的警告:

and if you were using gcc you would have seen at least this warning:

警告:多字符字符常量[-Wmultichar]

warning: multi-character character constant [-Wmultichar]

和可能这样的警告,以及溢出:

and probably this warning as well on overflow:

警告:溢出隐不断转换[-Woverflow]

warning: overflow in implicit constant conversion [-Woverflow]

这是获得是因为更有趣的值的字符常量的有它不能随便服用的第一个字符的整数值,在多字符常量的必须采取的整数的值,然后被转换成的字符的。 帮忙,提供了更详细的警告:

the value that y obtains is a little more interesting since character constant has an integer value it can not just be taking the first character, the multi-character constant has to take an integer value and then be converted to char. clang helpfully provides a more detailed warning:

警告:从'诠释'到'符'隐式转换改变了从3158320值48 [-Wconstant转换]

warning: implicit conversion from 'int' to 'char' changes value from 3158320 to 48 [-Wconstant-conversion]

GCC 产生相同的价值,我们可以从这个简单的一块code的看到当前版本:

and current versions of gcc produces the same value, as we can see from this simple piece of code:

printf("%d\n",'010');

那么,这是否 3158320 从何而来?对于 GCC 至少,如果我们看的的实现定义的行为它说:

so where does 3158320 comes from? For gcc at least, if we look at the documentation for Implementation-defined behavior it says:

编译器在每次计算一个多字符字符常量的字符,移由每个目标人物的位数离开了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.

如果我们执行的操作(假设8位字符的)文件中,我们发现:

if we perform the operation(assuming 8-bit char) document above we see:

 48*2^16 + 49*2^8 + 48  = 3158320
 ^         ^
 |         decimal value of ASCII '1'
 decimal value of ASCII '0'

GCC INT 字符使用convert模量 2 ^ 8 无论字符签署符号有效地留给我们最后的 8 位或 48

gcc will convert the int to char using modulus 2^8 regardless of whether char is signed or unsigned which effectively leaves us with the last 8 bits or 48.

这篇关于解释反斜杠用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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