String concatenation in C involves appending characters from a source string onto the end of a destination string starting at its null terminator. Using unbounded strcat() risks buffer overflow crashes if the destination array lacks sufficient memory capacity.
Safe String Concatenation Code Example
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[30] = "Lynxbee ";
const char *src = "Engineering Platform";
// Calculate remaining available buffer space in destination
size_t dest_len = strlen(dest);
size_t dest_capacity = sizeof(dest);
size_t max_copy = dest_capacity - dest_len - 1; // Reserve 1 byte for '\0'
// Perform safe bounded concatenation using strncat
strncat(dest, src, max_copy);
printf("Concatenated Result: %s (Length: %zu)\n", dest, strlen(dest));
return 0;
}
Comments and corrections