Namara

Code daily. Without assist.

2026-08-21

buffer.c

#include <string.h>

int main(void) {
    char buf[6];
    const char *name = "namara";

    for (int i = 0; i <= strlen(name); i++) {
        buf[i] = name[i];
    }
}

This overflows buf by one byte. Find it and fix it with a minimal change.

Answer

Make room for the terminator: char buf[7];. "namara" is 6 bytes plus a NUL, so it needs 7. The loop's <= is correct — it's meant to copy the NUL too — but buf was one byte too small to receive it.