HOWTO · C

How to Use the strdup Function in C

Learn how strdup duplicates a string in C, how to check and free its allocated copy, and when to use strndup or GNU strdupa.

On this page

strdup makes a null-terminated copy of a C string and returns separately allocated memory. Check the returned pointer before using it, then release a successful copy with free. Use strndup when you need to limit the number of copied bytes; strdupa is a GNU-specific alternative with a different lifetime.

What strdup Does and Where It Is Available

The declaration for strdup is char *strdup(const char *s);, in <string.h>. It copies the bytes of the null-terminated string s, including its terminating null byte, into newly allocated storage. The returned pointer refers to that storage; if allocation fails, the function returns NULL.

strdup is not part of the C standard library. It is specified by POSIX and is provided by common C libraries such as the GNU C library. A platform may require a feature-test macro to expose the declaration when compiling in a strict language mode. Check the target platform’s documentation when portability beyond POSIX systems matters.

Duplicate a String with strdup

Define _POSIX_C_SOURCE before including headers when the platform uses it to expose the POSIX declaration. The example checks the result before reading it and releases the copy after use. In the successful run, it prints the source and its duplicate.

Run this example:

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    const char *source = "C strings";
    char *copy = strdup(source);

    if (copy == NULL) {
        puts("Could not duplicate the string.");
        return EXIT_FAILURE;
    }

    printf("Source: %s\nCopy: %s\n", source, copy);
    free(copy);
    return 0;
}
Source: C strings
Copy: C strings

If strdup returns NULL, do not pass the pointer to string functions or call free on it. The failure branch above reports the problem and exits cleanly. Once the copy is no longer needed, call free exactly once; freeing the source literal or using the copy after freeing it is not valid.

Copy at Most n Bytes with strndup

strndup is also specified by POSIX and declared in <string.h>. Its declaration is char *strndup(const char *s, size_t n);. It copies no more than n bytes from s and always adds a terminating null byte to the allocated result. The limit counts bytes, not characters, so a multibyte encoded character may be cut partway through when the limit falls inside its encoding.

For example, strndup("C strings", 3) produces the string "C s". Its returned storage is dynamically allocated, so check for NULL and release a successful result with free, just as with strdup. The limit controls the amount copied, not the lifetime or ownership of the result.

Understand the GNU-Specific strdupa Alternative

strdupa is a GNU C library extension, not a POSIX or standard C function. Define _GNU_SOURCE before system headers when needed to expose its declaration. Like strdup, it accepts a null-terminated string and creates a copy, but it uses automatic storage associated with the current function rather than returning a heap allocation.

That storage lasts only while the containing function is active. Do not return the pointer for use after the function ends, and do not pass it to free. Its availability and implementation depend on the GNU environment, so use strdup when you need a POSIX interface or a copy whose lifetime you control with free. strdupa is not demonstrated as a runnable example here because it is specific to GNU environments.