strdup() function in C Programming Language

Rumman Ansari   Software Engineer   2019-03-31   7799 Share
☰ Table of Contents

Table of Content:


strdup() function in C duplicates the given string. Syntax for strdup( ) function is given below.

Syntax

char *strdup(const char *string);

Important Note

strdup() function is non standard function which may not available in standard library in C.

Program

In this program, string "atnyla" is duplicated using strdup() function and duplicated string is displayed as output.


#include <stdio.h>
#include <string.h>
int main()
{
    char *p1 = "atnyla";
    char *p2;
    p2 = strdup(p1);
 
    printf("Duplicated string is : %s", p2);
    return 0;
}

Output

Duplicated string is : atnyla