UTF-8 encodes each Unicode code point as 1 to 4 bytes. In C, a UTF-8 string is just a char array ending in \0, so functions like strlen() count bytes, not characters. You must treat the bytes as a variable-length encoding yourself.
Treat the string as bytes, use the lead byte to find sequence lengths, and skip continuation bytes when you count characters.
Keep text in char arrays (or unsigned char when decoding) and end with \0.
Use strlen() for the byte length, for buffers and memcpy().
Count code points by counting bytes that are not continuation bytes: (b & 0xC0) != 0x80.
Decode with the lead byte pattern to get the numeric code point.
Never cut a string in the middle of a multi-byte sequence.
Need
Use
Caveat
Byte length
strlen()
Not the character count
Code point count
Skip 10xxxxxx bytes
Not the same as visible glyphs
Simple ASCII tests
ctype.h functions
Only valid for single bytes
Full Unicode text
A library such as ICU or utf8proc
Normalization, case, width
Pseudocode
Pseudocode
for each byte b in the string:
if (b AND 0xC0) != 0x80: // not a continuation byte
code_point_count = code_point_count + 1
lead byte tells the length:
0xxxxxxx -> 1 110xxxxx -> 2 1110xxxx -> 3 11110xxx -> 4
Reference
Cheat Sheet
Rules of thumb for UTF-8 strings in C.
Topic
Detail
strlen(s)
Bytes before \0
Continuation byte
(b & 0xC0) == 0x80
ASCII compatibility
Bytes below 0x80 are plain ASCII
\0 byte
Never appears inside a valid sequence
wchar_t
16 bits on Windows, 32 bits on Linux and macOS
Output
Terminal and locale must also expect UTF-8
Trace
Worked Walkthrough — The String héllo
The literal "h\xC3\xA9llo" is the text “héllo” written with explicit bytes so it does not depend on your source file encoding.
Calculation
byte index: 0 1 2 3 4 5
byte value: 68 C3 A9 6C 6C 6F
meaning: h [ e-acute ] l l o
strlen -> 6 bytes
continuation bytes (10xxxxxx) -> only A9
code points -> 6 - 1 = 5
Slicing at byte 2 would cut e-acute in half and leave invalid UTF-8.
Code
C Examples
Compile with gcc file.c -std=c11 -o out. The examples use explicit hex escapes so they behave the same on any source encoding. For wide strings see wchar.h.
Example 1 — Count Bytes and Code Points
Compare strlen() with a loop that skips continuation bytes.
Limit 2. Byte 2 is A9, a continuation byte, so the cut moves back to 1 (just h).
Limit 3. Byte 3 is l, a lead byte, so 3 bytes (h and the full é) are safe.
Limit 9. The string is shorter than the limit, so the whole 6 bytes are kept.
Idea. Check the byte after the cut, and back up while it is a continuation byte.
Edge Cases & Pitfalls
UTF-8 bugs usually come from treating bytes as characters.
strlen
strlen is not a character count
It returns bytes. A 5-letter word with one accented letter has length 6.
ctype
ctype.h on multibyte text
isalpha, toupper, and friends work on single bytes only. They do not understand multi-byte letters.
slice
Cutting mid-character
Copying a fixed number of bytes can leave half a sequence, which shows as garbage or breaks parsers.
invalid
Invalid or overlong input
Decoders must reject stray continuation bytes, truncated sequences, and overlong forms from untrusted input.
wchar
wchar_t is not UTF-8
Its size and encoding depend on the platform. Convert at the edges with mbstowcs or a library.
console
Terminal and locale
A Windows console or a non-UTF-8 locale can print correct bytes as garbage. Check the code page and locale.
Analysis
Performance Notes
Task
Cost
Notes
Byte length
O(n)
strlen scans for \0
Count code points
O(n)
One pass, no allocation
Random access to nth character
O(n)
Variable width means no direct index
Because characters are variable width, finding the nth code point needs a scan. Keep byte offsets when you need fast random access, and convert only when an API requires fixed-width characters.
Remember
Key Takeaways
Encoding: UTF-8 uses 1 to 4 bytes per code point.
Length:strlen returns bytes, not characters.
Counting: skip bytes of the form 10xxxxxx.
Safety: validate input and never split a sequence.
One line: in C, UTF-8 is just bytes; you provide the character logic.
Frequently Asked Questions
Yes in practice. A UTF-8 string is a normal null-terminated char array. The standard library does not interpret the multi-byte structure for you.
strlen counts bytes up to the null terminator. Letters like é use two bytes, so the byte count is larger than the character count.
Count the bytes that are not continuation bytes, meaning those where (byte & 0xC0) != 0x80. This counts code points.
No. wchar_t is a fixed-width type whose size and encoding vary by platform, usually UTF-16 on Windows and UTF-32 on Linux and macOS.
Only for ASCII bytes. They operate on single bytes and will not handle multi-byte letters correctly.
A library such as ICU or utf8proc for normalization, case mapping, and grapheme handling. The C standard library covers only basics.