3.6. strcat
char *strcat( char * restrict s1, const char * restrict s2);strcat concatenates string s2 to end of string s1, and returns s1.
strcat can be written using strlen and strcpy:
strcpy( &s1[strlen(s1)], s2);Since strcat and strcpy return their first argument, the function return value can be used to simplify the writing of certain code.
For example, say we have a PC directory name and a file name, and we want to construct a complete path name:
char dir[80] = "C:\\OSP\\A3"; char fname[80] = "X.C"; char path[80];We want to set path to "C:\\OSP\\A3\\X.C" which requires copying the directory name, appending a backslash, then appending the filename:
strcpy( path, dir); strcat( path, "\\"); strcat( path, fname);This can be done in one statement, using the strcpy and strcat return values as arguments to strcat:
strcat( strcat( strcpy( path, dir), "\\"), fname);