strcat in C language – String Library Functions

strcat-in-c-with-example-programs

Introduction:

In earlier articles, We looked at the strlen function and strcpy functions. In today’s article, We will look at the strcat in C Programming language with example programs.

📢 This Program is part of the C String Practice Programs series

Prerequisites:

It is recommended to go through the following string tutorials to better understand this article.

strcat in C Programming Language:

The strcat function is used to concatenate two strings. The strcat() function appends the source string at the end of the destination string. Let’s look at the strcat function syntax.

Syntax of strcat function in C:

Where

dest – Destination String

src – Source String

As we can see from the above syntax of the strcat function, The strcat function appends the source string ( src) at the end of the Destination string ( dest).

The strcat() function overwrites the NULL Character( \0) at the end of the destination string ( dst), Appends the src string and finally adds the terminating NULL Character( \0) at end of the appended string (final string).

The destination string( dest) size must be large enough to accommodate the concatenated string size, Otherwise the strcat() function behavior is undefined.

Function Parameters:

The strcat() function takes two strings as the parameters. The first argument is the Destination string (i.e dst) and the second argument is Source string (i.e src).

Function Return Type:

The strcat() function returns the destination string back to the caller.

📢 The resulting string of the strcat() function is always terminated with the NULL Character.

Program to concatenate two strings using strcat function in C:

⚠️ Don’t forget to include the string.h header file.

Program Explanation:

In the above program, We declared two strings called src and dest with the size of MAXSIZE. The MAXSIZE is a global constant and it’s value is 100. So our strings at max can store 100 characters (including the terminating NULL Character)

We prompted the user to provide the two strings and stored the given strings in the src and dest strings respectively. We used the gets function to read the strings ( The gets function won’t do the boundary checks, so be cautious while using it)

Then we used the strcat() function to append the source string src to the destination string dest

strcat(dest, src);

Finally, We printed the resultant string on the console.

Program Output:

Let’s compile and run the program.

Test1:

In the above example, The user provided the source string as the Sillycodes.com and the destination string as the Welcome to (added extra space at after to). The strcat function concatenated two strings and returned the resulting result as  Welcome to  Sillycodes.com

Example 2:

strcat-function-in-c-program-output

Similarly, The strcat function appended two strings World and Hellocreated the Hello World string.

One final thing to note is the strcat function also returns the resultant string to the caller. So we can use the nested strcat function and directly use it in the printf functions, etc.

strcat(str1, str(str2, str3));

and

printf("Resultant string is : %s\n", strcat(dest, src));

Related Articles:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] strcat library function in C […]

Leave a Reply