Multiple characters in a character constant

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

Big endian (AIX, POWER, IBM compiler)

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

Big endian (Solaris, Sparc, SUN compiler)

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

Little endian (Linux, x86_64, gcc)

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

Little endian (Solaris, x86_64, Sun compiler)

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

Leave a Comment