C Programming Tutorial 0/65 lessons ~6 min read Lesson 27

    String Functions

    String functions from and — prefer bounds-checked snprintf and fgets over unbounded strcpy, strcat, and removed gets() to prevent buffer overflo…

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    String functions from <string.h> and <stdio.h> — prefer bounds-checked snprintf and fgets over unbounded strcpy, strcat, and removed gets() to prevent buffer overflows in C17/C23.

    Understanding the topic

    strlen Returns length excluding null terminator (size_t).

    strcmp Compare lexicographically — 0 if equal.

    • strlen — Returns length excluding null terminator (size_t).
    • Buffer overflow prevention (snprintf vs strcpy/strcat) — Avoid unbounded strcpy/strcat and never use gets() (removed in C11/C17/C23).
    • strcmp — Compare lexicographically — 0 if equal.

    Step-by-step explanation

    1. strlen — Returns length excluding null terminator (size_t).
    2. Buffer overflow prevention (snprintf vs strcpy/strcat) — Avoid unbounded strcpy/strcat and never use gets() (removed in C11/C17/C23).
    3. strcmp — Compare lexicographically — 0 if equal.

    Syntax reference

    Syntax reference:

    c
    int n = snprintf(dst, sizeof dst, "%s", src);

    Informative example

    Example program:

    c
    #include <stdio.h>
    #include <string.h>
    int main(void) {
    char dst[16];
    int written = snprintf(dst, sizeof dst, "%s %s", "Hello", "C23");
    if (written < 0 || (size_t)written >= sizeof dst) {
    fprintf(stderr, "Truncated!\n");
    return 1;
    }
    printf("%s len=%zu\n", dst, strlen(dst));
    return 0;
    }

    Output

    Hello C23 len=9

    Execution workflow

    1String Functions — step by step
    1 / 3

    strlen

    Returns length excluding null terminator (size_t).

    Code examples

    Buffer overflow prevention (snprintf vs strcpy/strcat)

    Avoid unbounded strcpy/strcat and never use gets() (removed in C11/C17/C23). Use snprintf(dst, sizeof dst, ...) to guarantee NUL termination and detect truncation.

    c
    char buf[8];
    int n = snprintf(buf, sizeof buf, "%s", user_input);
    bool truncated = (n < 0 || (size_t)n >= sizeof buf);

    Best practices

    • Enable warnings: gcc -Wall -Wextra -std=c11 source.c -o app
    • Give every variable a defined value before it is read.
    • Stay inside array bounds — C will not stop you from over-running a buffer.

    Common mistakes

    • strcpy/strcat buffer overflow when destination is too small
    • strncpy does NOT NUL-terminate when src length >= n
    • gets() was removed from ISO C (C11/C17/C23) — always use fgets()

    Hands-on exercise

    Practice problems:

    • Rewrite an unsafe strcpy/strcat snippet using snprintf and check for truncation
    • Trigger a deliberate stack-buffer-overflow in a scratch file and catch it with -fsanitize=address
    • Read a line safely with fgets() and strip the trailing newline

    Summary

    String Functions in C — Common helpers from string.

    Ready to mark this lesson complete?Track your journey across the entire course.