Loading...

C essentialsData

Strings & string.h functions

12 min lesson

Next
  • A string in C is a sequence of characters stored in a char array and ending with a special zero character called the null terminator, written '\0'.
  • Strings matter because almost every program handles text: names, messages, file paths and user input.
  • Think of a string as a row of letter tiles with a special 'stop' tile at the end, so the program knows where the text ends.
  • The null terminator '\0' marks the end of the string; without it, functions keep reading past the array and cause bugs.
  • The length of a string is the number of characters before the null terminator; it does not include the terminator itself.
  • string.h provides ready-made functions: strlen (length), strcpy (copy), strcat (append), strcmp (compare), and more.
  • The computer reads characters one by one until it sees '\0', which is why the terminator is so important.
  • The syntax is: char name[20] = "Ada"; or const char *greeting = "Hello"; for a read-only string literal.
  • A realistic use: storing a username, comparing a typed password, or building a message by joining words.
  • A surprising detail: comparing two strings with == compares their addresses, not their letters; you must use strcmp.
  • The example program stores a name, measures its length, copies it, appends a greeting and compares two strings.
Keep in mind
  • Every string must end with '\0'; leave room for it when sizing arrays.
  • Use strcmp to compare strings, not ==.
  • Use strncpy or snprintf when copying to avoid buffer overflows.
  • Remember that the length from strlen does not include the null terminator.
  • Text handling is a huge part of programming. You are doing great!
main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    // A char array big enough for the text and the '\0' terminator
    char name[20] = "Ada";
    // A separate buffer to hold a copy
    char copy[20];

    // Measure the length of the string (without the terminator)
    printf("Length: %zu\n", strlen(name));

    // Copy the string into the other buffer
    strcpy(copy, name);
    // Print the copy to prove it worked
    printf("Copy: %s\n", copy);

    // Append a greeting to the copy (room is left for the terminator)
    strcat(copy, " Lovelace");
    // Print the combined string
    printf("Full name: %s\n", copy);

    // Compare two strings by their letters, not their addresses
    if (strcmp(name, "Ada") == 0) {
        // strcmp returns 0 when the strings are equal
        printf("Match found\n");
    }

    return 0;
}

What happens: Length: 3 Copy: Ada Full name: Ada Lovelace Match found

Breaking it down

String literal initialization

char name[20] = "Ada";

Copies the characters 'A', 'd', 'a' and '\0' into the array. The array is bigger than needed, which is fine and leaves room for growth.

strlen

strlen(name)

Counts characters until it reaches '\0' and returns 3. The terminator itself is not counted.

strcpy

strcpy(copy, name);

Copies the characters of name, including '\0', into copy. The destination must be big enough to hold them.

strcat

strcat(copy, " Lovelace");

Finds the '\0' in copy and appends the new text there. The destination must have enough free space.

strcmp

strcmp(name, "Ada") == 0

Compares the letters one by one. It returns 0 when the strings are equal, a negative value if the first is smaller, and positive if larger.

Common string.h functions

FunctionWhat it doesExample
strlenLength without '\0'strlen("Ada") == 3
strcpyCopy a stringstrcpy(dst, src)
strncpyCopy with a limitstrncpy(dst, src, n)
strcatAppend a stringstrcat(dst, "!")
strcmpCompare stringsstrcmp(a, b) == 0
strchrFind a characterstrchr(s, 'a')

A helpful analogy

A string is like a row of letter tiles with a special stop tile at the end. When you read the row, you keep going until you see the stop tile. If the stop tile is missing, you keep reading past the end and pick up nonsense from the next row.

Nesting rules

  • 1.Always leave room for the '\0' terminator when sizing a char array.
  • 2.Use strcmp to compare strings; == compares addresses, not contents.
  • 3.Use strncpy or snprintf when copying to a fixed buffer to avoid overflow.
  • 4.Never modify a string literal like "Hello"; store it in a const char * instead.

Common mistakes

Comparing strings with ==

Avoid

if (name == "Ada") // compares addresses

Use instead

if (strcmp(name, "Ada") == 0)

== compares the memory addresses of the two strings, not their letters. Use strcmp, which compares character by character.

Forgetting the null terminator space

Avoid

char s[3] = "Ada"; // no room for '\0'

Use instead

char s[4] = "Ada";

The string needs 3 letters plus the terminator. Without room for '\0', functions read past the array and misbehave.

Using strcpy into a small buffer

Avoid

char dst[4]; strcpy(dst, "Hello"); // overflow

Use instead

char dst[6]; strcpy(dst, "Hello");

strcpy copies everything including '\0' and does not check the destination size, so a small buffer overflows.

Quick tips

  • •Print strings with %s; it stops at the '\0' automatically.
  • •Use sizeof to get the array size and strlen to get the text length; they differ.
  • •Prefer snprintf over strcat for building strings safely.
  • •Try changing name to a longer string and see how the length changes.
  • •Use -fsanitize=address to catch string buffer overflows during testing.
Previous
Next