How to concatenate two strings in C?
In C, the easiest way to concatenate two strings is to use the strcat() function whose prototype is shown below:
char * strcat( char * destination, const char * source );
Parameters:
- dest is a pointer to the destination string which must also contain the first string. This string must be large enough to contain the result of the concatenation.
- src is the string to add to the dest string.
Returned value: the function returns a pointer to the result string (dest)
The function adds the string source to the end of the string destination. In a symbolic way : destination = destination + source . Here is an example:
#include <stdio.h>
#include <string.h>
int main()
{
char src[] = "begin";
char dest[50] = "end";
strcat(dest, src);
// Affiche : "beginend"
printf(dest);
return 0;
}
The strcat() function belongs to the string.h library. The function returns the address of the string resulting from the concatenation (a pointer to the string destination). This can be used to cascade multiple calls to the strcat() function:
// Concaténe str1 + str2 + str3 => str1
strcat (str1, strcat(str2, str3) );
In C, the easiest way to concatenate two strings is to use the strcat() function whose prototype is shown below:
char * strcat( char * destination, const char * source );
Parameters:
- dest is a pointer to the destination string which must also contain the first string. This string must be large enough to contain the result of the concatenation.
- src is the string to add to the dest string.
Returned value: the function returns a pointer to the result string (dest)
The function adds the string source to the end of the string destination. In a symbolic way : destination = destination + source . Here is an example:
#include <stdio.h>
#include <string.h>
int main()
{
char src[] = "begin";
char dest[50] = "end";
strcat(dest, src);
// Affiche : "beginend"
printf(dest);
return 0;
}
The strcat() function belongs to the string.h library. The function returns the address of the string resulting from the concatenation (a pointer to the string destination). This can be used to cascade multiple calls to the strcat() function:
// Concaténe str1 + str2 + str3 => str1
strcat (str1, strcat(str2, str3) );
In C, the easiest way to concatenate two strings is to use the strcat() function whose prototype is shown below:
char * strcat( char * destination, const char * source );
Parameters:
- dest is a pointer to the destination string which must also contain the first string. This string must be large enough to contain the result of the concatenation.
- src is the string to add to the dest string.
Returned value: the function returns a pointer to the result string (dest)
The function adds the string source to the end of the string destination. In a symbolic way : destination = destination + source . Here is an example:
#include <stdio.h>
#include <string.h>
int main()
{
char src[] = "begin";
char dest[50] = "end";
strcat(dest, src);
// Affiche : "beginend"
printf(dest);
return 0;
}
The strcat() function belongs to the string.h library. The function returns the address of the string resulting from the concatenation (a pointer to the string destination). This can be used to cascade multiple calls to the strcat() function:
// Concaténe str1 + str2 + str3 => str1
strcat (str1, strcat(str2, str3) );
| # | ID | Query | URL | Count |
|---|---|---|---|---|
| 0 | 13361 | alphons | https://en.ans.wiki/5749/how-to-concatenate-two-strings-in-c | 1 |
| 1 | 12433 | en | https://en.ans.wiki/5749/how-to-concatenate-two-strings-in-c | 11 |