Question: I haven't long time in touch with C language. I have some questions related to chinese words and strncpy. 

```
char* testString = "你好嗎?"
sizeof(testString) => it prints out 4.
strlen(testString) => it prints out 10.

```

When i want to copy to another char array, i have some issue.

char msgArray[7]; /\* This is just an example. Due to some limitation, we have limited the buffer size. \*/

If i want to copy the data, i need to check 

```
if (sizeof(testString) < sizeof(msgArray)) {
    strncopy(msgArray, testString, sizeof(msgArray));
}

```

It will have problem. The result is it will only copy a partial data. 

Actually it should have compared with 

```
if (strlen(testString) < sizeof(msgArray)) {

}
else {
   printf("too long");
}

```

But i don't understand why it happened. 

If i want to define to limit the characters count (including unicode (eg. chinese characters), how can i achieve to define the array? I think i can't use the char[] array. 

Thanks a lot for all the responses.

My workaround solution: I finally decide to cut the strings to meet the limited bytes.

Answer: 
The behaviour of `char* testString = "你好嗎?"` depends on the compiler. One option would be to investigate what your compiler is doing by outputting individual characters via `%d` . It might be generating a UTF-8 literal.

In the C11 standard you may write one of the following:

```
char const *testString = u8"你好嗎?";   // UTF-8 encoding

```

or

```
wchar_t const *testString = u"你好嗎?"; // UTF-16 or UCS-4 encoding

```

With these strings, there is no way in Standard C to work with *Unicode characters*. You can only work with code points and/or C characters. `strlen` or `wcslen` respectively will give the number of C characters in the string but this might not correspond to how many glyphs are displayed.

---

If your compiler does not comply with the latest standard (i.e. it gives errors for the above lines) then to write portable code you will need to only use ASCII in your sourcefile.

To embed unicode in string literals you could use `'\xNN'` with UTF-8 hex codes.

---

In both cases your best bet is probably to use a third-party Unicode library such as ICU.

---

For the second part of the question, I'll assume you are using UTF-8. The result of `strlen(testString) + 1` is the number of characters you need to copy. You say you are stuck with a fixed-size 7-byte buffer. If that is true then the code could be: 

```
char buf[7];

if ( strlen(testString) > 6 )
    exit(1);   // or jump to some other error handling

strcpy(buf, testString);

```

The `strncpy` should be avoided because it does not null-terminate its buffer in some circumstances; you can always replace it with `strcpy` or `snprintf`.
normally you can use wchar\_t to represent UTF characters (non-English characters), and each character may need 2 or 4 bytes. And if you really want to count number of characters in a quick way, use uint32\_t(unsigned int) instead of char/wchar\_t because UTF32 is guarantee each character (including non-English character) will have the same size of 4 bytes.

sizeof(testString) will only give you the size of a pointer itself which is 4 in 32bit system and 8 in 64bit system.

use wcslen to get the string len if you're using wchar\_t; if you're using uint32\_t, you need to write your own strlen function similar as follow:

```
size_t strlenU32(const uint32_t *s) {
    const uint32_t *u = s;
    while (*u) u++;
    return u - s;
}

```