Table of contents
1.
Introduction
2.
What is strcat() function in C?
3.
Syntax of strcat() Function in C
4.
Parameters of strcat() in C
5.
Example of strcat() function in C
5.1.
Implementation
5.1.1.
Explanation
6.
Return value of strcat() Function in C
7.
Exceptions of strcat() Function in C
8.
Frequently Asked Questions
8.1.
What is the function of strcpy () and strcat ()?
8.2.
What is the use of strlen () and strcat ()?
8.3.
What is the strcat function and the strncat function?
8.4.
What is the main difference between strcpy () and strcat ()?
9.
Conclusion
Last Updated: Oct 7, 2024
Medium

strcat() function in c

Author Sagar Mishra
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The strcat() function in C is used to concatenate (join) two strings. It appends the source string to the end of the destination string, modifying the destination. The function ensures that the resulting string is null-terminated. Commonly used for string manipulation, strcat() requires that the destination array has enough space to hold the combined strings.

strcat() function in C

So, Today we will discuss the complete process of the strcat function in C.

Also See, Sum of Digits in C and C Static Function.

What is strcat() function in C?

The strcat function within the C programming language is an inherent function designed for merging two strings together. Put simply, it accepts two strings as input and merges them into a unified string. This function is defined in the string.h header file and holds substantial utility across software development and programming tasks.

Syntax of strcat() Function in C

strcat( string1, string2);
You can also try this code with Online C Compiler
Run Code

 

  • strcat() accepts two parameters.
  • Both parameters must be of string datatype.
  • We need to declare the header file #include<string.h> to use strcat() inbuilt string function in C.
     

The general syntax for strcat() function in is:

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

Parameters of strcat() in C

The parameters that we pass to the strcat function are as follows:

  • dest: The dest is the character pointer that we pass to the strcat function where we want the source string to get appended to, it is important to note that the destination string should have enough space to hold the final string after appending,for the append operation to execute correctly.
  • src: The src is the source character pointer we pass to the strcat function, it refers to the string that we want to append to the destination string.

Example of strcat() function in C

strcat() function example

Must Read Passing Arrays to Function in C

Implementation

First, let's check out the working of concatenation without using the strcat function in C.

#include <stdio.h>
#include<string.h>
int main ()
{
    //Declaring the value of str1 and str2
    char str1[50] = "Coding";
    char str2[50] = "Ninjas";
    int i, j;
    for (i = 0; str1[i] != '\0'; i++)
    {
        //making use of variable i
    }
    for (j = 0; str2[j] != '\0'; j++)
    {
        str1[i] = str2[j];
        i++;
    }
    //printing the value of str1
    printf ("%s", str1);
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

CodingNinjas


Let's now understand the concept of the strcat function in C by taking the below example.

#include <stdio.h>
#include<string.h>
int main()
{
    //Declaring the value of str1 and str2
    char str1[50] = "Coding"; 
    char str2[50] = "Ninjas";
    strcat (str1, str2);  //performing the function
    printf("%s", str1);   //printing the value of str1
    return 0;
}
You can also try this code with Online C Compiler
Run Code


Output

CodingNinjas

 

Explanation

We have seen both methods using the strcat function in C and without using the strcat function in C. As you can see, the first code looks verbose compared to the second one. Also, it will take less space compared to the first example.

You can also read about the dynamic arrays in c, Short int in C Programming and  Tribonacci Series 

Return value of strcat() Function in C

In C, the `strcat()` function appends the characters from the source string to the end of the destination string, modifying it in place. It does not return a value, so you work with the altered destination string for the concatenated result.

Exceptions of strcat() Function in C

The `strcat()` function in C has certain exceptions and limitations to be aware of. It assumes that the destination string has enough space to accommodate the concatenated result. If the destination string is not large enough, it can lead to buffer overflow and undefined behavior. Additionally, both the destination and source strings must be null-terminated. 

Failing to null-terminate either string can result in incorrect concatenation or program crashes. Always ensure sufficient memory and proper null-termination when using `strcat()`.

Frequently Asked Questions

What is the function of strcpy () and strcat ()?

The `strcpy()` function in C copies the contents of one string to another. `strcat()` appends (concatenates) one string to the end of another.

What is the use of strlen () and strcat ()?

The `strlen()` function in C is used to determine the length of a string (the number of characters). `strcat()` appends one string to the end of another.

What is the strcat function and the strncat function?

In C, `strcat()` is used to concatenate strings by appending the source string to the destination. `strncat()` does the same but limits the number of characters appended.

What is the main difference between strcpy () and strcat ()?

The main difference between strcpy() and strcat() is that strcpy() copies the source string into the destination, replacing its existing content, while strcat() appends the source string to the end of the destination string, preserving its original content and adding the new string.

Conclusion

The strcat() function in C is a fundamental tool for string manipulation, allowing developers to concatenate strings efficiently. While it simplifies combining strings, it's essential to ensure that the destination buffer has enough space to prevent buffer overflows. Proper usage of strcat() contributes to robust and error-free string handling in C programs. We hope this blog has helped you enhance your knowledge of the strcat function in C. 

Recommend Reading:

Refer to our Guided Path on code360 to upskill yourself!

Live masterclass