在字符常量多个字符 [英] Multiple characters in a character constant

查看:115
本文介绍了在字符常量多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些C编译器允许在字符常量多个字符。
  这意味着,写作,而不是是是很可能不被发现。
  资料来源:C陷阱和缺陷

Some C compilers permit multiple characters in a character constant. This means that writing 'yes' instead of "yes" may well go undetected. Source: C traps and pitfalls

谁能给在多个字符允许的字符常量这样的例子?

Can anyone give an example of this where multiple characters are allowed in a character constant?

推荐答案

由于code猴引,这是实现定义和实现不同 - 它不只是一个大尾端/ LittleEndian和字符集的区别。我与程序测试4实现(全部采用ASCII)

As Code Monkey cited, it is implementation defined and implementation varies -- it isn't just a BigEndian/LittleEndian and charset difference. I've tested four implementations (all using ASCII) with the program

#include <stdio.h>

int main()
{
    unsigned value = 'ABCD';
    char* ptr = (char*)&value;

    printf("'ABCD' = %02x%02x%02x%02x = %08x\n", ptr[0], ptr[1], ptr[2], ptr[3], value);
    value = 'ABC';
    printf("'ABC'  = %02x%02x%02x%02x = %08x\n", ptr[0], ptr[1], ptr[2], ptr[3], value);
    return 0;
}

和我有四个不同的结果。

and I got four different results

大端(AIX,POWER,IBM编译器)

Big endian (AIX, POWER, IBM compiler)

'ABCD' = 41424344 = 41424344
'ABC'  = 00414243 = 00414243

大端(Solaris和SPARC上,Sun编译)

Big endian (Solaris, Sparc, SUN compiler)

'ABCD' = 44434241 = 44434241
'ABC'  = 00434241 = 00434241

小尾数(Linux操作系统,x86_64的,GCC)

Little endian (Linux, x86_64, gcc)

'ABCD' = 44434241 = 41424344
'ABC'  = 43424100 = 00414243

小尾数(Solaris中,x86_64的,Sun编译)

Little endian (Solaris, x86_64, Sun compiler)

'ABCD' = 41424344 = 44434241
'ABC'  = 41424300 = 00434241

这篇关于在字符常量多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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