概述
x转义的定义是这样的
转义符 | 字符值 | 输出结果 |
xh[h...] | 具有此十六进制码的字符 | 输出此字符 |
问题
看似x后面可以接受1或n个十六进制的字符,但是如果你把一个超过ff分十六进制数赋值给一个char变量,会得到一个"Out of range"的提示;
#include <stdio.h> #include <wchar.h> int main(void) { char a = 'x123'; printf("%d ", a); }
c28.c: In function ¡®main¡¯: c28.c:10:11: warning: hex escape sequence out of range [enabled by default] char a = 'x123';
分析
这是因为char类型只能容纳1byte的数,面对十六进制123是无能为力的,但是,即使是把char类型换成wchar_t类型,貌似也会有问题;
#include <stdio.h> #include <wchar.h> int main(void) { wchar_t a = 'x123'; printf("%d ", a); }
c28.c: In function ¡®main¡¯: c28.c:10:14: warning: hex escape sequence out of range [enabled by default] wchar_t a = 'x123';
但如果用L预处理符号以表明一下这个/段字符是宽字符,就不会出现问题。这是因为声明字符变量时如果不表明该字面量是宽字符,编译器默认会当成窄字符去处理,而一个窄字符的大小只有1byte
#include <stdio.h> #include <wchar.h> int main(void) { wchar_t a = L'x123'; printf("%d ", a); }
291
当然,宽字符(也就是wchar_t类型)也有大小限制,这通常视系统而定,一般系统一个宽字符的大小是4byte。如果你输入的十六进制值大于4byte,那么编译器也会报"Out of range";
另外,x跟字符位数无关,也就是说x61、x061和x000000061三者是一个意思
#include <stdio.h> #include <wchar.h> int main(void) { char a = 'x0000000061'; printf("%d ", a); }
97
总结
x确实可以接受1或n个十六进制数,只是要看赋值变量类型是否匹配
题外话
其他语言跟C会有一点差异,例如在javascript和PHP,x只会接受前两位十六进制数,后面(要是有)则当普通字符处理。