C String Literal“对于角色来说太大了”

时间:2022-01-02 16:47:33

With MSVC 2010 i try to compile this in C or C++ mode (needs to be compilable in both) and it does not work. Why? I thought and found in the documentation that '\x' takes the next two characters as hex characters and not more (4 characters when using \X").

在MSVC 2010中,我尝试在C或C ++模式下编译它(需要在两者中都可编译)并且它不起作用。为什么?我想在文档中发现'\ x'将接下来的两个字符作为十六进制字符而不是更多(使用\ X时为4个字符)。

I also learned that there is no portable way to use character codes outside ASCII in C source code anyway, so how can i specify some german ISO-8859-1 characters?

我还了解到,无论如何都没有可移植的方法在C源代码中使用ASCII之外的字符代码,那么如何指定一些德语ISO-8859-1字符呢?

int main() {
     char* x = "\xBCd";  // Why is this not char(188) + 'd'
}

// returns  test.c(2) : error C2022: '3021' : too big for character
// and a warning with GCC

1 个解决方案

#1


13  

Unfortunately you've stumbled upon the fact that \x will read every last character that appears to be hex1,2, instead you'll need to break this up:

不幸的是,你偶然发现了\ x将读取每个看起来像是hex1,2的最后一个字符的事实,而不是你需要解决这个问题:

const char *x = "\xBC" "d"; /* const added to satisfy literal assignment probs */

Consider the output from this program:

考虑一下这个程序的输出:

/* wide.c */
#include <stdio.h>
int main(int argc, char **argv) 
{
    const char *x = "\x000000000000021";
    return printf("%s\n", x);
}

Compiled and executed:

编译并执行:

C:\temp>cl /nologo wide.c
wide.c

C:\temp>wide
!
  1. Tested on Microsoft's C++ compiler shipped with VS 2k12, 2k10, 2k8, and 2k5
  2. 在VS 2k12,2k10,2k8和2k5附带的Microsoft C ++编译器上进行了测试

  3. Tested on gcc 4.3.4.
  4. 测试了gcc 4.3.4。

#1


13  

Unfortunately you've stumbled upon the fact that \x will read every last character that appears to be hex1,2, instead you'll need to break this up:

不幸的是,你偶然发现了\ x将读取每个看起来像是hex1,2的最后一个字符的事实,而不是你需要解决这个问题:

const char *x = "\xBC" "d"; /* const added to satisfy literal assignment probs */

Consider the output from this program:

考虑一下这个程序的输出:

/* wide.c */
#include <stdio.h>
int main(int argc, char **argv) 
{
    const char *x = "\x000000000000021";
    return printf("%s\n", x);
}

Compiled and executed:

编译并执行:

C:\temp>cl /nologo wide.c
wide.c

C:\temp>wide
!
  1. Tested on Microsoft's C++ compiler shipped with VS 2k12, 2k10, 2k8, and 2k5
  2. 在VS 2k12,2k10,2k8和2k5附带的Microsoft C ++编译器上进行了测试

  3. Tested on gcc 4.3.4.
  4. 测试了gcc 4.3.4。