C strcat() function
C strcat() function - concatenate two strings
The strcat() function concatenates string2 to string1 and ends the resulting string with the null character.
Use strcat() when:
- Concatenating strings in cases where you need to build sentences or combine multiple strings.
- Appending suffixes or additional information to an existing string.
- Building formatted output strings by gradually adding content to a base string.
Syntax:
char *strcat(char *string1, const char *string2);
Parameters:
Name | Description | Required /Optional |
---|---|---|
string1 | Null-terminated destination string. | Required |
string2 | Null-terminated source string. | Required |
Return value from strcat()
The strcat() function shall return string1. No return value is reserved to indicate an error.
Examples: strcat() function
Example 1: Combine strings
The existing example uses strcat() to append " Programming" to the end of string1, which initially contains "C". The result is "C Programming", showcasing how strcat() can combine strings to form a larger phrase.
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 40
int main(void)
{
char string1[SIZE] = "C";
char * ptr;
ptr = strcat( string1, " Programming" );
printf( "string1 = %s\n", string1 );
}
Output:
string1 = C Programming
Example 2: Concatenating multiple strings
This example demonstrates using strcat() to join multiple words into a sentence.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define an empty buffer with enough space to hold the final sentence
char sentence[50] = "Hello,";
// Append each word to the sentence
strcat(sentence, " world");
strcat(sentence, " of");
strcat(sentence, " C programming!");
// Display the concatenated sentence
printf("Concatenated Sentence: %s\n", sentence);
return 0;
}
Output:
Concatenated Sentence: Hello, world of C programming!
Explanation:
- A buffer sentence is initialized with the word "Hello,".
- Each subsequent word is appended to sentence using strcat().
- The final sentence, "Hello, world of C programming!" is printed, demonstrating strcat()'s capability to create complete sentences from multiple parts.
Example 3: Creating a full path from directory and file names
This example demonstrates using strcat() to form a file path by combining a directory name and a file name.
Code:
#include <stdio.h>
#include <string.h>
int main() {
// Define a directory path and a file name
char path[100] = "/home/user/documents";
char filename[] = "/file.txt";
// Append the file name to the directory path
strcat(path, filename);
// Display the complete path
printf("Complete Path: %s\n", path);
return 0;
}
Output:
Complete Path: /home/user/documents/file.txt
Explanation:
- The path string holds a directory location.
- filename stores the file name to be added to the directory.
- strcat() appends filename to path, forming the complete file path "/home/user/documents/file.txt", which is then printed.
C Programming Code Editor:
Previous C Programming: C memset()
Next C Programming: C strncat()
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming/string/c-strcat.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics