105. Strings and Character Handling-ii

Question.4

A firmware engineer needs to copy a command string into an 8-byte buffer, guaranteeing null termination and no overflow. Which approach is safest?

Option A:

strcpy(buf, src);

Option B:

strncpy(buf, src, 8);

Option C:

strncpy(buf, src, 7);
buf[7] = '\0';

Option D:

snprintf(buf, sizeof(buf), "%s", src);

Which options guarantee both no overflow and null termination?

Need Help? Refer to the Quick Guide below

Select Answer