strdup() Function in C: Duplicating Strings

Rumman Ansari   Software Engineer   2024-07-05 04:29:02   7706 Share
Subject Syllabus DetailsSubject Details
☰ 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