Data Structures · Pointers and Strings
String Library Functions: strcat and strncat
Overview
C's standard string library (string.h) already provides ready-made string functions; you do not write them yourself — you #include <string.h> and call them directly.
This section covers two concatenation functions, strcat and strncat, including how they overwrite the destination's null terminator and what they return.
Key concepts
strcat(s, t)— concatenates stringtto the end of strings.strncat(s, t, n)— concatenates only the firstncharacters oftto the end ofs.- Null termination — every string in C is null terminated (
'\0'at the end); without the null character we would not know where the string ends. - Parameters — both functions take character pointers (
char *); they take the strings pointed to by these pointers and append appropriately. - Return value — both return a pointer to the destination, i.e. the starting address of
s. - Space requirement — when
sis a character array, it must have enough space to hold the appended characters; if there is no space,tcannot be accommodated and the append fails (buffer overflow).

How it works
strcat(s, t)
- Take two strings
sandt, e.g.s=r a v i \0,t=b a b u \0. - The intention is to concatenate
tto the end ofs. tremains unchanged;schanges.- The null terminator of
sis overwritten: from the position where'\0'was, the characters oftare copied one by one. - A new null is placed at the end, because every string must be null terminated.
- The function returns a pointer to the destination — the starting address of
s.
strncat(s, t, n)
- Works exactly like
strcat, with one difference: it does not append the entire stringt. - It appends only
ncharacters oftto the end ofs. - It also returns
char *, the same kind of return asstrcat.
Formulas
- Space needed in
sforstrcat:strlen(s) + strlen(t) + 1 - Space needed in
sforstrncat:strlen(s) + n + 1 - Return value of both: pointer to the destination (start of
s)
Worked examples
strcat example
Initial state:
| String | Contents (with null) |
|---|---|
| s | r a v i \0 |
| t | b a b u \0 |
Step 1 — locate the null of s (after i).
Step 2 — copy t starting at that position, overwriting the null:
| String | Contents after strcat |
|---|---|
| s | r a v i b a b u \0 |
| t | b a b u \0 (unchanged) |
Step 3 — return value = starting address of s.
strncat example (n = 2)
s = r a v i \0, t = b a b u \0, n = 2 → only b a is appended:
s becomes r a v i b a \0.
Comparison
| Feature | strcat(s, t) | strncat(s, t, n) |
|---|---|---|
| Characters appended | Entire string t | Only n characters of t |
Effect on t | Unchanged | Unchanged |
Effect on s | Null of s overwritten, all of t copied, new null at end | Null of s overwritten, n characters of t copied, new null at end |
| Return value | Pointer to destination (start of s) | Pointer to destination (start of s) |
| Parameters | Two char * | Two char * plus n |
Space needed in s | Enough for strlen(s) + strlen(t) + 1 | Enough for strlen(s) + n + 1 |
Revision summary
- Include
string.hto use these library functions; they are already provided in C. - All strings must be null terminated; concatenation overwrites the null of the destination and places a new null at the end.
strcatappends the whole ofttos;tis not modified, onlyschanges.strncatappends onlyncharacters oft.- Both return a pointer to the destination (the starting address of
s). - The destination character array must have sufficient space to hold the appended characters, otherwise the append cannot be accommodated (buffer overflow).
Previous-year exam insights
- strncat always appends a terminating '\0' after the copied characters, so it copies at most n characters from t and then writes one null; the destination must therefore hold strlen(s) + n + 1 bytes (and if t is shorter than n, only strlen(t) characters plus the null are written).
Asked inGATE 2015ISRO 2020 - strlen counts characters up to but excluding the terminating '\0', whereas sizeof on a character array gives the total number of bytes in the array including the '\0';
for char a[] = "..." initialized by a string literal, sizeof(a) = strlen(a) + 1.
Asked inISRO 2020 - A string literal used to initialize a char array of exactly fitting size (e.g. char s[5] = "1234") stores the characters plus the '\0';
if the array size is smaller than strlen+1 the null is dropped, and if larger the remaining bytes are zero-initialized.
Asked inGATE 2015 - Modifying characters through a pointer into a char array (e.g. p = s + k; *p = c) changes the array itself, and printf("%s", s) prints from the start of the array until the first '\0', so only characters before the first null appear.
Asked inGATE 2015 - strcat and strncat require the destination to be a modifiable, writable array with enough room; the source t must be null terminated, and the result is undefined if source and destination overlap.
- Pointer arithmetic on char pointers advances by one byte per increment, so s + k points to the (k+1)-th character of the array (0-based index k).
Asked inGATE 2015