C String strcat() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<string.h>

What You’ll Learn

The strcat() function appends one C-string to another. It finds the null terminator of the destination, copies the source after it, and writes a new '\0'. It is one of the most common ways to build longer strings from smaller pieces.

01

Append

Add src to dest.

02

Modifies dest

Result in place.

03

Returns dest

Same pointer.

04

Needs space

Buffer must fit.

05

string.h

Standard C.

06

vs strncat

Safer limit.

Definition and Usage

strcat stands for string concatenate. The destination dest must already contain a valid null-terminated string—often initialized as an empty string with char buf[64] = ""; or char buf[64] = {0};.

The function walks dest until it finds '\0', then copies every character from src including the final null terminator. There is no length check: you are responsible for making sure the buffer is large enough.

💡
Beginner Tip

Before calling strcat(dest, src), ask: “Does dest have room for strlen(dest) + strlen(src) + 1 bytes?” If not, use a larger buffer or a bounded function like strncat or snprintf.

📝 Syntax

Standard C declaration:

C
char *strcat(char *dest, const char *src);

Parameters

  • dest — modifiable buffer that already holds a C-string.
  • src — string to append (not modified).

Return Value

  • Returns dest (pointer to the destination buffer).
  • The concatenated string is stored in dest; no new string is allocated.

Header

  • #include <string.h>

⚡ Quick Reference

dest (before)srcdest (after)
"Hello, ""World!""Hello, World!"
"" (empty)"C""C"
"/home/user""/docs""/home/user/docs"
required sizeanystrlen(dest) + strlen(src) + 1 bytes
Append
strcat(dest, src);

In-place

Empty start
char buf[32] = "";

Valid dest

Size check
len + slen + 1 <= cap

Stay safe

Bounded
strncat(dest, src, n)

Limit chars

Examples Gallery

Compile with gcc strcat.c -std=c11 -o strcat. Every example uses a char array large enough for the result.

📚 Getting Started

Append one string to another in a fixed-size buffer.

Example 1 — Basic Concatenation

Append "World!" to "Hello, ".

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char destination[20] = "Hello, ";
    const char *source = "World!";

    strcat(destination, source);

    printf("Concatenated String: %s\n", destination);

    return 0;
}

How It Works

strcat locates the '\0' after "Hello, ", copies "World!" starting there, and terminates the result. The array must hold at least 14 characters including the null byte.

Example 2 — Start From an Empty String

Build a message from scratch by appending words one at a time.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char message[64] = "";

    strcat(message, "Learning ");
    strcat(message, "C ");
    strcat(message, "strings");

    printf("%s\n", message);

    return 0;
}

How It Works

An empty C-string is just '\0' at index 0. Each strcat appends after the current end, so repeated calls grow the same buffer.

📈 Practical Patterns

Paths, chaining, and using the return value.

Example 3 — Build a File Path

Combine a directory and a filename into one path string.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char path[128] = "/home/user";
    const char *folder = "/projects";
    const char *file = "/main.c";

    strcat(path, folder);
    strcat(path, file);

    printf("Full path: %s\n", path);

    return 0;
}

How It Works

Real programs often assemble paths or URLs from parts. Always size the buffer for the worst-case combined length. For production code, snprintf can be easier to audit than multiple strcat calls.

Example 4 — Chained strcat() Calls

Nest calls using the fact that strcat returns dest.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[32] = "Code";

    strcat(strcat(strcat(buf, "To"), "Fun"), "!");

    printf("%s\n", buf);

    return 0;
}

How It Works

Each inner call returns buf, so the next append continues from the updated string. This works but is hard to read—separate statements or snprintf are usually clearer.

Example 5 — Using the Return Value

Pass the result directly to printf.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char line[48] = "Name: ";

    printf("%s\n", strcat(line, "Alex"));

    return 0;
}

How It Works

strcat returns dest, so you can use it wherever a char* is expected. Remember that line is modified before printf runs.

🚀 Common Use Cases

  • Greeting messages — combine a prefix with a user name.
  • File paths — join directory and file name segments.
  • Log lines — append timestamps, levels, and text into one buffer.
  • CSV or text output — add commas and fields to a row buffer.
  • Protocol strings — build simple command lines in embedded systems.

🧠 How strcat() Works

1

Find end of dest

Scan dest until '\0' is found.

Locate
2

Copy src byte by byte

Write each character from src starting at that position.

Copy
3

Write final \0

The last copied byte from src is the null terminator.

Terminate
=

char* dest

Return dest pointing at the longer combined string.

📝 Notes

  • dest must be writable—not a string literal like "hello".
  • Leave room for strlen(dest) + strlen(src) + 1 bytes.
  • dest and src must not overlap.
  • No bounds checking—overflow corrupts memory and is a common bug.
  • Prefer strncat, snprintf, or dynamic allocation for unknown lengths.

⚡ Optimization

Library strcat implementations scan dest from the start every time, so repeated appends to the same buffer cost O(n²) total if you keep re-walking the growing prefix. Track the write position or use stpcpy/snprintf when building long strings in a loop.

Conclusion

strcat() is the standard way to append one C-string to another in place. Initialize dest correctly, size your buffer, and consider safer alternatives when input length is unpredictable.

Next, learn strncat() to limit how many characters get appended.

💡 Best Practices

✅ Do

  • Use a char array with known capacity
  • Initialize dest as "" or {0} before first append
  • Check remaining space before each strcat
  • Use strncat or snprintf when lengths vary
  • Keep src as const char* when possible

❌ Don’t

  • Append to string literals
  • Assume the buffer is big enough
  • Let dest and src overlap
  • Forget the final null terminator requirement on dest
  • Chain many strcat calls in performance-critical loops without tracking length

Key Takeaways

Knowledge Unlocked

Five things to remember about strcat()

Use these points when joining strings in C.

5
Core concepts
📝 02

In Place

Modifies dest.

Storage
🔢 03

Size Matters

No bounds check.

Safety
📈 04

Returns dest

Chainable.

Return
💬 05

vs strncat

Limit length.

Compare

❓ Frequently Asked Questions

strcat() appends the source string src to the end of the destination string dest. It finds the null terminator of dest, copies src starting there, and adds a new null terminator. The result is stored in dest itself.
char* strcat(char* dest, const char* src); Include <string.h>. dest must be a modifiable char array (or heap buffer) that is already a valid C-string with a null terminator.
It returns dest—the same pointer you passed as the first argument. This allows chaining like strcat(strcat(buf, "a"), "b"), though building strings step by step with snprintf is often clearer.
No. strcat() does not know how large dest is. You must ensure the buffer has enough free space for the existing text plus src plus the final '\0'. Writing past the end causes buffer overflow.
strcat() appends the entire src string. strncat(dest, src, n) appends at most n characters from src and always null-terminates the result (when there is room). strncat is safer when you track remaining buffer space.
No. If dest and src overlap, behavior is undefined. Use memmove-style logic or separate buffers when strings might share memory.
Did you know?

The name strcat comes from string concatenate. C89 standardized it alongside strcpy and strlen. Modern style guides often recommend snprintf for formatting because one call can express both copy and length limits clearly.

Continue the String Functions Series

Master in-place concatenation with strcat(), then learn bounded appends with strncat().

Next: strncat() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful