Appending one string at the end of another - strcat

The strcat function is used to concatenate one string (source) at the end of another string (destination). It does the following:

Takes the destination string.

Finds the NULL character.

Copies the source string starting at the location of the NULL character of destination string.

Appends a NULL character to the destination string when all characters of the source string are copied to the destination string.

Here is an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <stdio.h>

#include <string.h>

#define DEST_SIZE 40

intmain()

{

charsrc[]="Look Here";

chardest[DEST_SIZE]="Unimaginable";

strcat(dest,src);

printf(dest);

return0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Notice how it works. When dest is initialized with char dest[DEST_SIZE] = "Unimaginable"; there is a NULL character at the end. That's the starting point for the source string to be copied. When all characters of source string are copied to dest, a NULL character is appended.

Warning: The destination character array must be large enough to hold all characters of source, all characters of destination and a NULL character. Following ill-formed code triggers undefined behaviour:

Warning: The destination character array must be initialized with C string before passing it to strcat. In other words, the destination character array must have at least 1 location which has a NULL character. Following is an ill-formed code and shouldn't be practiced:

At minimum the destination array could be initialized as empty string (only the NULL character) and after that strcat can be used:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <stdio.h>

#include <string.h>

#define DEST_SIZE 40

intmain()

{

chardest[DEST_SIZE]="";

strcat(dest,"Look Here");

printf(dest);

return0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If the destination character array is declared at global scope, then all elements of the array are initialized to 0 automatically by C. Since a 0 is equivalent to NULL character, an array declared globally can be directly passed to strcat:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <stdio.h>

#include <string.h>

#define DEST_SIZE 40

chardest[DEST_SIZE];

intmain()

{

strcat(dest,"Look Here");

printf(dest);

return0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Character pointers can be used in strcat:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

#include <stdio.h>

#include <string.h>

#define DEST_SIZE 40

intmain()

{

charsrc[]="Look Here";

chardest[DEST_SIZE]="Unimaginable";

char*ps=src+4;

char*pd=dest+6;

strcat(pd,ps);

printf(dest);

return0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In the example above, ps points to the 5th character of src and pd points to the 7th character of dest. strcat finds the NULL character of dest starting from pd (i.e. the 7th character of dest). This will find the NULL character at the end of dest - the same NULL character will be used for any other pointers inside dest. Copying starts from the 5th character of src, this is what ps is pointing to. At the end a NULL character is appended.

Warning: When using character pointers, care must be taken so that the source and destination aren't overlapped. Following is an example of ill-formed code which uses strcat of overlapped buffer - which causes undefined behaviour:

String concatenation upto n characters - strncat

The strncat function is used to append at most n characters from source string to destination string. Here is an example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#include <stdio.h>

#include <string.h>

#define DEST_SIZE 40

intmain()

{

charsrc[]="Look Here";

chardest[DEST_SIZE]="Unimaginable";

strncat(dest,src,4);

printf(dest);

return0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The example above will append first 4 characters from src at the end of dest (i.e. starting from the NULL character of dest) and appends a NULL character in the end.

Warning: The destination character array must be large enough to hold all characters of destination, n characters of source and a NULL character. Following is an example of an ill-formed code which violates this rule:

In the example above, ps points to the 5th character of src (that's the space). The call to strncat finds the NULL character of dest, appends 5 characters starting from ps and finally appends a NULL character.

Warning: When using character pointers be careful to the fact that the source and the destination cannot be overlapped. If the source and destination overlaps, this will trigger undefined behaviour. Following is such an example of bad coding:

If the number of characters to copy is more than the characters in the source string, strncat will stop appending when it will encounter the NULL character in source. In the example below strncat stops concatenation as soon as it reaches the NULL character of src: