Lesson 4 of 7
String Functions - The Complete Safety Guide
The `<string.h>` functions are fast but unforgiving - most unsafe variants (`strcpy`, `strcat`, `gets`) are classic sources of buffer overflows that have led to real security vulnerabilities (Heartbleed, Morris worm, etc.). Every C programmer must know the safe alternatives and understand **why** the unsafe versions are dangerous.
string.h - Safe vs Unsafe + All Important Functions
string.h - Safe vs Unsafe + All Important Functions
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
char dst[16];
const char *src = "Hello, World!";
/* ── strlen - length WITHOUT the null terminator ────── */
size_t len = strlen(src); /* 13 */
printf("strlen = %zu
", len);
/* ── strcpy - UNSAFE if dst too small ───────────────── */
/* strcpy(dst, src); ← if src > sizeof(dst)-1: OVERFLOW */
/* SAFE alternative: */
strncpy(dst, src, sizeof(dst) - 1);
dst[sizeof(dst) - 1] = '