A learning brother asked me how to connect two characters, think of java/python inside can be directly connected with +, but there is no such a convenient way to do?
The answer is that there is a magical function called strcat in the String.h Library of C, which can do this. Let's start with the explanation below ~ ~ ~
At this point we may want to know what its prototype consists of:
extern Char *strcat (charconstchar *src);
As we can see, the prototype of the function is a pointer to two char types, which is defined in Chinese as follows:
char * strcat (target string, source string ); //Appends a copy of the source string to the destination string, terminates the null character in the destination string Fu Yuyuan The first character of the string, and joins the two strings to form a new string that contains a null char at the end.
In C, the function prototype exists in the <string.h> header file
Parameter definition
- dest --points to the target array, which contains a C string and is sufficient to hold the appended string.
- src --points to the string to append, which does not overwrite the target string.
The function returns a pointer to the final target string dest
As an example:
#include <stdio.h>#include<string.h>intMain () {Chard[ -] ="Goldenglobal"; Char* s ="View"; strcat (d,s); printf ("%s", D); GetChar (); return 0;}
The printing results are as follows:
Let me give you an easy-to-understand example:
#include <stdio.h>#include<string.h>intMain () {Charstr[ the]; strcpy (str,"these"); strcat (str,"Strings"); strcat (str," is"); strcat (str,"concatenated."); Puts (str); return 0;}
The printing results are as follows:
These strings is concatenated.
Application of connection function strcat (simple explanation) in C + +