C Language: strcat function
(String Concatenation)
In the C Programming Language, the strcat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1. It returns a pointer to s1 where the resulting concatenated string resides.
Syntax
The syntax for the strcat function in the C Language is:
char *strcat(char *s1, const char *s2);
Parameters or Arguments
- s1
- A pointer to a string that will be modified. s2 will be copied to the end of s1.
- s2
- A pointer to a string that will be appended to the end of s1.
Returns
The strcat function returns a pointer to s1 (where the resulting concatenated string resides).
Required Header
In the C Language, the required header for the strcat function is:
#include <string.h>
Note
- Use the strcat function with caution as it is easy to concatenate more bytes into your variable using the strcat function, which can cause unpredictable behavior.
Applies To
In the C Language, the strcat function can be used in the following versions:
- ANSI/ISO 9899-1990
strcat Example
Let's look at an example to see how you would use the strcat function in a C program:
/* Example using strcat by TechOnTheNet.com */ #include <stdio.h> #include <string.h> int main(int argc, const char * argv[]) { /* Define a temporary variable */ char example[100]; /* Copy the first string into the variable */ strcpy(example, "TechOnTheNet.com "); /* Concatenate the following two strings to the end of the first one */ strcat(example, "is over 10 "); strcat(example, "years old."); /* Display the concatenated strings */ printf("%s\n", example); return 0; }
When compiled and run, this application will output:
TechOnTheNet.com is over 10 years old.
Similar Functions
Other C functions that are similar to the strcat function:
Advertisements