[ create a new paste ] login | about

Link: http://codepad.org/B3MXOgqc    [ raw code | output | fork ]

C, pasted on Sep 11:
/*
 * A program that illustrates strings in C.  Designed for C99, but should run
 * fine in C90 with a lot of warnings.
 */

#include <stdio.h>
#include <string.h>
#include <wchar.h>

/* Simple strings from the basic character set */
char s1[] = {'d', 'o', 'g', (char)0};
char s2[] = {'d', 'o', 'g', '\0'};
char* s3 = "dog";
wchar_t* s4 = L"dog";

/* String with some non-ascii, but still "8-bit" characters */
char* s5 = "c\xe9ili";
char* s6 = "c\u00e9ili";
char* s7 = "c\U000000e9ili";
wchar_t* s8 = L"c\U000000e9ili";

/* Strings with characters with codepoints > 0xFF */
char* s9 = "k\u014dpa`a";
wchar_t* s10 = L"k\u014dpa`a";

void inspectString(char* s) {
    int i, n;
    printf("[%s] length=%d codepoints=[ ", s, strlen(s));
    for (i = 0, n = strlen(s)+1; i < n; i++) {
        printf("%02x ", (unsigned char)s[i]);
    }
    printf("]\n");
}

void inspectWideString(wchar_t* s) {
    int i, n;
    printf("[%ls] length=%d codepoints=[ ", s, wcslen(s));
    for (i = 0, n = (wcslen(s)+1)*sizeof(wchar_t); i < n; i++) {
        printf("%02x ", ((unsigned char*)s)[i]);
    }
    printf("]\n");
}

int main() {
    inspectString(s1);
    inspectString(s2);
    inspectString(s3);
    inspectString((char*)s4);
    inspectWideString(s4);
    inspectString(s5);
    inspectString(s6);
    inspectString(s7);
    inspectString((char*)s8);
    inspectWideString(s8);
    inspectString(s9);
    inspectString((char*)s10);
    inspectWideString(s10);
    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
[dog] length=3 codepoints=[ 64 6f 67 00 ]
[dog] length=3 codepoints=[ 64 6f 67 00 ]
[dog] length=3 codepoints=[ 64 6f 67 00 ]
[d] length=1 codepoints=[ 64 00 ]
[dog] length=3 codepoints=[ 64 00 00 00 6f 00 00 00 67 00 00 00 00 00 00 00 ]
[c�ili] length=5 codepoints=[ 63 e9 69 6c 69 00 ]
[céili] length=6 codepoints=[ 63 c3 a9 69 6c 69 00 ]
[céili] length=6 codepoints=[ 63 c3 a9 69 6c 69 00 ]
[c] length=1 codepoints=[ 63 00 ]
[63 00 00 00 e9 00 00 00 69 00 00 00 6c 00 00 00 69 00 00 00 00 00 00 00 ]
[kōpa`a] length=7 codepoints=[ 6b c5 8d 70 61 60 61 00 ]
[k] length=1 codepoints=[ 6b 00 ]
[6b 00 00 00 4d 01 00 00 70 00 00 00 61 00 00 00 60 00 00 00 61 00 00 00 00 00 00 00 ]


Create a new paste based on this one


Comments: