'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Sep 22, 2023
Medium

strcat() function in c

Author Sagar Mishra
2 upvotes
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Hey Ninjas! Have you ever thought of merging two words using a few lines of code? What do you think? Is this possible? If it is possible, then how can we do this? I know you have many questions like this in your mind. The answer to all these is Yes. The process of joining more than two strings is known as Concatenation. We can join two strings using the strcat function in C.

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.

strcat function in C

The strcat function in C adds a blueprint of string2 at the end of string1. The resultant concatenated string is stored in string1 and is returned as a pointer there. 

The syntax of the strcat function in C is shown below.

strcat( string1, string2);

 

  • 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);
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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;
}


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;
}


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 

Must Read what is storage class in c and Decision Making in C

Return value of strcat() 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() 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.

Conclusion

This article discusses the strcat function in C. In detail, we have seen the definition of the strcat function in C. Along with this, we've seen the example, which includes two codes, one with the strcat function and the other without the strcat function.

We hope this blog has helped you enhance your knowledge of the strcat function in C. If you want to learn more, check out our articles.

And many more on our platform Coding Ninjas Studio.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

Check out this article - C++ String Concatenation

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Previous article
strspn() in C
Next article
Strcmp() Function in C
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass