1. Gyanastra
  2. Data Structures
  3. Pointers and Strings
  4. String Library Functions: strcat and strncat
Free lesson, no account needed 5 min read 10 practice questions after it
Asked in past papers5 times
  • GATE CSE201520131995
  • ISRO2020
  • UGC NET2005

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 string t to the end of string s.
  • strncat(s, t, n) — concatenates only the first n characters of t to the end of s.
  • 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 s is a character array, it must have enough space to hold the appended characters; if there is no space, t cannot be accommodated and the append fails (buffer overflow).
Data structure diagram: String Library Functions: strcat and strncat
Figure. String Library Functions: strcat and strncat

How it works

strcat(s, t)

  1. Take two strings s and t, e.g. s = r a v i \0, t = b a b u \0.
  2. The intention is to concatenate t to the end of s.
  3. t remains unchanged; s changes.
  4. The null terminator of s is overwritten: from the position where '\0' was, the characters of t are copied one by one.
  5. A new null is placed at the end, because every string must be null terminated.
  6. 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 string t.
  • It appends only n characters of t to the end of s.
  • It also returns char *, the same kind of return as strcat.

Formulas

  • Space needed in s for strcat: strlen(s) + strlen(t) + 1
  • Space needed in s for strncat: strlen(s) + n + 1
  • Return value of both: pointer to the destination (start of s)

Worked examples

strcat example

Initial state:

StringContents (with null)
sr a v i \0
tb a b u \0

Step 1 — locate the null of s (after i). Step 2 — copy t starting at that position, overwriting the null:

StringContents after strcat
sr a v i b a b u \0
tb 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

Featurestrcat(s, t)strncat(s, t, n)
Characters appendedEntire string tOnly n characters of t
Effect on tUnchangedUnchanged
Effect on sNull of s overwritten, all of t copied, new null at endNull of s overwritten, n characters of t copied, new null at end
Return valuePointer to destination (start of s)Pointer to destination (start of s)
ParametersTwo char *Two char * plus n
Space needed in sEnough for strlen(s) + strlen(t) + 1Enough for strlen(s) + n + 1

Revision summary

  • Include string.h to 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.
  • strcat appends the whole of t to s; t is not modified, only s changes.
  • strncat appends only n characters of t.
  • 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
Pages 1–2 of 2