The string functions in C strlen, strcpy and strcmp come from the string.h header. strlen returns the number of characters before the null terminator, strcpy copies a string including its terminator into a destination buffer, and strcmp compares two strings and returns 0 when they are equal. All three trust the terminator, so they are only safe when the destination is large enough and the strings are properly terminated.
This guide shows each function in a complete program, explains the return values and covers the safer alternatives to use in current code.
size_t strlen(const char *s) counts characters up to, but not including, the '\0'. The return type is size_t, an unsigned type, so print it with %zu.
strlen walks the whole string every time, so its cost grows with the length. Store the result in a variable when you need it more than once, rather than calling it in a loop condition.
char *strcpy(char *dest, const char *src) copies src, terminator included, into dest. It does not check that dest is big enough. You must make sure of that yourself.
The check strlen(src) < sizeof dest leaves room for the terminator. Without it, a long source overflows dest, which is undefined behaviour and a well-known source of security bugs.
strncpy(dest, src, n) looks like the safe version, but it has a trap: if src has n or more characters, it does not add a terminator. If src is shorter, it pads the rest with zeros, which wastes time. A correct use needs a manual terminator:
A more convenient option is snprintf(dest, sizeof dest, "%s", src);, which always terminates and truncates safely. C23 does not add strlcpy, though many systems provide it as an extension, and POSIX adopted it in a recent revision, so check your platform before relying on it.
strcat(dest, src) appends src to the end of dest. It needs room for both strings plus the terminator, and dest must already hold a valid string.
Repeated strcat calls rescan the destination every time, so building large strings this way is slow. Track the end position yourself or use snprintf with an offset.
int strcmp(const char *a, const char *b) compares character by character. It returns 0 if the strings are equal, a negative value if a sorts before b and a positive value if it sorts after. The exact magnitude is not specified, so only test the sign or compare to 0.
Never use == to compare strings. It compares pointer addresses, not contents, so two equal strings in different arrays give false. The comparison is case sensitive and uses byte values; for case-insensitive comparison use strcasecmp on POSIX systems or _stricmp on Windows, as the standard has no such function.
| Function | Purpose | Returns | Main danger |
|---|---|---|---|
strlen(s) | Count characters | size_t length | Unterminated string |
strcpy(d, s) | Copy string | d | Destination too small |
strncpy(d, s, n) | Copy up to n bytes | d | May not terminate |
strcat(d, s) | Append string | d | Destination too small |
strcmp(a, b) | Compare | Negative, 0, positive | Using == instead |
strchr(s, c) | Find a character | Pointer or NULL | Not checking NULL |
The return value is a difference indicator, and zero means no difference. That makes if (strcmp(a, b)) true when the strings are different, which surprises beginners, so write == 0 explicitly.
Not by itself. It can leave the destination without a terminator, so set the last byte manually or prefer snprintf.
No. strlen("abc") is 3 and the array needs 4 bytes.
No. A const char * literal is read-only, so copy into a char array or a heap buffer instead.
Use strlen for length, strcpy or snprintf for copying, strcat for appending and strcmp for ordering and equality. Check buffer sizes before every copy, never compare strings with ==, and remember that every one of these functions depends on a correct '\0' terminator.