Strings and Runes in C
Our program demonstrates working with strings and characters in C. Here’s the full source code:
This C program demonstrates working with wide characters and strings to handle Unicode text. Let’s break it down:
We include necessary headers and set the locale to handle Unicode characters properly.
We define a constant wide string
s
containing Thai characters.We print the length of the string in bytes. In C, we use
wcslen()
to get the number of wide characters and multiply bysizeof(wchar_t)
to get the byte count.We iterate over the string to print the hexadecimal values of each wide character.
We count and print the number of characters using
wcslen()
.We iterate over the string again, printing each character’s Unicode code point and its starting byte position.
We demonstrate accessing individual characters and passing them to a function (
examine_char
).The
examine_char
function checks if the character matches specific Unicode values.
To compile and run this program:
Note that the exact output may vary depending on your system’s locale settings and wide character implementation.
This example demonstrates how to work with Unicode strings in C, which is a bit more complex than in some higher-level languages due to C’s lower-level nature. The use of wide characters (wchar_t
) and wide character functions allows us to handle Unicode text, although it requires more explicit management compared to languages with built-in Unicode support.