Home C++ Strings Functions strncat() C++ strncat() Function Definition In C++, strncat() adds text onto the end of a C-string, but it copies no more than count characters from the source. It always writes a '\0' after the characters it copies, so it may write one more byte than count.
All three arguments are required. dest must already have room for the old text, the new characters, and that extra '\0'. The two strings must not overlap. Include <cstring>.
Syntax The third argument limits how much of the second string is added.
strncat(dest, "World", 3); Call What it does strncat(dest, "World", 3)Adds at most 3 characters from World onto the end of dest.
Library declarations char* strncat(char* destination, const char* source, size_t count); Parameters Parameter Description dest Required. The C-string you add onto. It must have free space. source Required. The characters you copy from. count Required. The most characters to take from source.
Return Value Return Value Description dest The same pointer you passed in, now holding the longer string.
C++ Examples 1. Basic strncat 2. Limit characters 3. vs std::string Example 1 — Basic strncat() Append a short piece after "Hello ".
#include <iostream>
#include <cstring>
int main() {
char dest[50] = "Hello ";
const char* src = "World!";
strncat(dest, src, 5);
std::cout << dest << '\n';
return 0;
} How It Works count is 5, so only W o r l d is copied.The '!' is not appended. A '\0' is written automatically at the end. Example 2 — Take Only the First Few Letters Build a short greeting from a longer source.
#include <iostream>
#include <cstring>
int main() {
char message[40] = "Hi, ";
strncat(message, "C++ Tutorial!", 3);
std::cout << message << '\n';
return 0;
} How It Works Only the first 3 characters of the source are used. Result becomes "Hi, C++". Useful when you want a prefix, not the whole string. Example 3 — Clearer Style with std::string Append a limited substring without managing a buffer.
#include <iostream>
#include <string>
int main() {
std::string text = "Hi, ";
std::string src = "C++ Tutorial!";
text.append(src, 0, 3);
std::cout << text << '\n';
return 0;
} How It Works append(src, 0, 3) takes 3 characters from index 0.No manual '\0' or fixed array size. Learn strncat for C APIs; prefer std::string in new C++. Frequently Asked Questions What does strncat() do in C++? strncat() adds at most count characters from one C-string onto the end of another, then writes a null character.
What are the parameters of strncat()? dest is the string you add onto. source is the text you copy from. count is the maximum number of characters to copy.
What does strncat() return? The same pointer as dest.
Does strncat() add a null character? Yes. It always writes one after the copied characters, so dest needs one extra byte.
How is strncat() different from strcat()? strcat() copies the whole source. strncat() copies at most count characters.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion