TExcelTAD
Home › C › C String Functions

C String Functions

Since C strings are plain char arrays, you cannot use =, + or == on them. Instead, the standard header <string.h> provides a family of functions to measure, copy, join, compare and search strings. Include it at the top of your file:

c
#include <string.h>

The essential functions

Function What it does
strlen(s) number of characters before the '\0'
strcpy(dest, src) copies src (including '\0') into dest
strncpy(dest, src, n) copies at most n characters
strcat(dest, src) appends src to the end of dest
strncat(dest, src, n) appends at most n characters
strcmp(a, b) compares: 0 if equal, negative if a sorts first, positive otherwise
strncmp(a, b, n) compares only the first n characters
strchr(s, c) pointer to the first occurrence of character c, or NULL
strstr(s, sub) pointer to the first occurrence of substring sub, or NULL

Length: strlen

strlen counts characters up to (not including) the terminator. Compare it with sizeof, which measures the whole array:

c
char name[32] = "Margaret";
strlen(name);   // 8  - characters before the terminator
sizeof(name);   // 32 - size of the whole array
strlen("");     // 0  - the empty string

strlen returns a size_t, printed with %zu.

Copying and joining: strcpy and strcat

Because arrays cannot be assigned, strcpy copies text into an existing array. strcat appends to the end of what is already there:

c · try it yourself
#include <stdio.h>
#include <string.h>

int main(void) {
    char full[40];

    strcpy(full, "Ada");
    strcat(full, " ");
    strcat(full, "Lovelace");
    printf("Full name: %s (%zu chars)\n", full, strlen(full));

    char backup[40];
    strcpy(backup, full);
    full[0] = 'X';
    printf("full: %s, backup: %s\n", full, backup);
    return 0;
}
Full name: Ada Lovelace (12 chars)
full: Xda Lovelace, backup: Ada Lovelace

Warning: Neither strcpy nor strcat knows how big dest is. If the result does not fit, they write past the end of the array — a buffer overflow, the cause of countless security bugs. Always make sure the destination is large enough.

A safe and convenient alternative is snprintf, which formats into a buffer and never writes more than the size you give it:

c · try it yourself
#include <stdio.h>

int main(void) {
    char label[12];
    int n = snprintf(label, sizeof label, "%s-%d", "invoice", 20250);
    printf("label: %s\n", label);
    printf("needed %d chars, buffer holds %zu\n", n, sizeof label - 1);
    return 0;
}
label: invoice-202
needed 13 chars, buffer holds 11

The text was cut off safely, and the return value tells you how long it would have been, so you can detect truncation.

Comparing: strcmp

strcmp compares two strings character by character. It returns 0 when they are equal — easy to get backwards, since 0 usually means false:

c · try it yourself
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *a = "apple";
    const char *b = "apricot";
    const char *c = "apple";

    printf("a vs c: %s\n", strcmp(a, c) == 0 ? "equal" : "different");
    printf("a vs b: %s\n", strcmp(a, b) < 0 ? "a sorts first" : "b sorts first");
    printf("\"Zoo\" vs \"apple\": %s\n", strcmp("Zoo", "apple") < 0 ? "Zoo first" : "apple first");
    printf("first 2 chars of a and b equal? %s\n", strncmp(a, b, 2) == 0 ? "yes" : "no");
    return 0;
}
a vs c: equal
a vs b: a sorts first
"Zoo" vs "apple": Zoo first
first 2 chars of a and b equal? yes

Comparison uses character codes, so all uppercase letters (65–90) sort before lowercase ones (97–122). That is why "Zoo" comes before "apple".

Searching: strchr and strstr

These return a pointer into the original string where the match starts, or NULL if there is none. Subtracting the start of the string gives the index:

c · try it yourself
#include <stdio.h>
#include <string.h>

int main(void) {
    const char *email = "grace.hopper@navy.mil";

    const char *at = strchr(email, '@');
    if (at != NULL) {
        printf("'@' found at index %td\n", at - email);
        printf("domain: %s\n", at + 1);
    }

    const char *found = strstr(email, "hopper");
    printf("contains \"hopper\": %s\n", found ? "yes" : "no");
    printf("contains \"turing\": %s\n", strstr(email, "turing") ? "yes" : "no");
    return 0;
}
'@' found at index 12
domain: navy.mil
contains "hopper": yes
contains "turing": no

%td prints a ptrdiff_t, the type you get when subtracting two pointers.

Character helpers in <ctype.h>

For single characters, <ctype.h> offers tests and conversions such as isalpha, isdigit, isspace, toupper and tolower:

c · try it yourself
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char text[] = "Hello, World 2025!";
    int letters = 0, digits = 0;
    for (int i = 0; text[i] != '\0'; i++) {
        if (isalpha((unsigned char) text[i])) letters++;
        if (isdigit((unsigned char) text[i])) digits++;
        text[i] = toupper((unsigned char) text[i]);
    }
    printf("%s\nletters=%d digits=%d\n", text, letters, digits);
    return 0;
}
HELLO, WORLD 2025!
letters=10 digits=4

Tip: Cast to unsigned char before passing a char to <ctype.h> functions; negative values (non-ASCII bytes) are otherwise undefined behaviour.

Go further: C Buffers, Lengths and Unsafe Functions reviews strcpy, strcat, sprintf and gets as a security reviewer and shows the bounded replacements.

Test yourself

What does strcmp("cat", "cat") return?

  1. 1
  2. 0
  3. true
  4. -1

What is strlen("hello")?

  1. 4
  2. 5
  3. 6
  4. It depends on the array size

Why is strcpy(dest, src) dangerous?

  1. It deletes src
  2. It cannot copy spaces
  3. It does not check whether dest is big enough
  4. It only copies the first character