C strcat() Function

The strcat() function in C is a standard library function that concatenates two strings. It is part of the C standard library (string.h). This function is useful for appending one string to the end of another.

Table of Contents

  1. Introduction
  2. strcat() Function Syntax
  3. Understanding strcat() Function
  4. Examples
    • Concatenating Two Strings
    • Concatenating a String into a Struct
  5. Real-World Use Case
  6. Conclusion

Introduction

The strcat() function appends the source string to the destination string, overwriting the null terminator at the end of the destination string and then adding a new null terminator. The destination string must have enough space to hold the resulting concatenated string.

strcat() Function Syntax

The syntax for the strcat() function is as follows:

char *strcat(char *dest, const char *src);

Parameters:

  • dest: A pointer to the destination string to which the content is to be appended.
  • src: A pointer to the source null-terminated string to be appended.

Returns:

  • The function returns a pointer to the destination string (dest).

Understanding strcat() Function

The strcat() function takes two strings as input: the destination string and the source string. It appends the source string to the destination string, ensuring that the destination string remains null-terminated. The destination string must have enough space to accommodate the concatenated result to avoid buffer overflows.

Examples

Concatenating Two Strings

To demonstrate how to use strcat() to concatenate two strings, we will write a simple program.

Example

#include <stdio.h>
#include <string.h>

int main() {
    char dest[50] = "Hello, ";
    char src[] = "World!";

    // Concatenate the strings using strcat
    strcat(dest, src);

    // Print the concatenated string
    printf("Concatenated string: %s\n", dest);

    return 0;
}

Output:

Concatenated string: Hello, World!

Concatenating a String into a Struct

This example shows how to use strcat() to concatenate strings into a struct.

Example

#include <stdio.h>
#include <string.h>

typedef struct {
    int id;
    char first_name[20];
    char last_name[20];
    char full_name[40];
} Person;

int main() {
    Person p;
    p.id = 1;
    strcpy(p.first_name, "Ramesh");
    strcpy(p.last_name, "Fadatare");

    // Concatenate the first name and last name to form the full name
    strcpy(p.full_name, p.first_name);
    strcat(p.full_name, " ");
    strcat(p.full_name, p.last_name);

    // Print the struct
    printf("Person ID: %d, Full Name: %s\n", p.id, p.full_name);

    return 0;
}

Output:

Person ID: 1, Full Name: Ramesh Fadatare

Real-World Use Case

Building a File Path

In real-world applications, the strcat() function can be used to build file paths by concatenating directory names and file names.

Example: Building a File Path

#include <stdio.h>
#include <string.h>

int main() {
    char base_path[100] = "/home/user/documents";
    char file_name[] = "report.txt";

    // Ensure there's a slash between the base path and file name
    if (base_path[strlen(base_path) - 1] != '/') {
        strcat(base_path, "/");
    }

    // Concatenate the file name to the base path
    strcat(base_path, file_name);

    // Print the full file path
    printf("Full file path: %s\n", base_path);

    return 0;
}

Output:

Full file path: /home/user/documents/report.txt

Conclusion

The strcat() function is used for concatenating strings in C. By understanding and using this function correctly, you can efficiently combine strings and build complex string data in your programs. Always ensure that the destination string has enough space to accommodate the concatenated result to prevent buffer overflows and undefined behavior.

Comments