You are on page 1of 5

10/10/2020 strdup() and strndup() functions in C/C++ - GeeksforGeeks

strdup() and strndup() functions in C/C++


Last Updated: 01-04-2019

The strdup() and strndup() functions are used to duplicate a string.

strdup() :
Syntax : char *strdup(const char *s);
This function returns a pointer to a null-terminated byte string, which is a duplicate of the
string pointed to by s. The memory obtained is done dynamically using malloc and hence it
can be freed using free().
Time to unleash full potential! Experience unlimited, unhindered learning with GeeksforGeeks
It returns a pointerPremium! Only 1000string
to the duplicated seatssavailable,
. Grab Yours Now!

Below is the C implementation to show the use of strdup() function in C:

// C program to demonstrate strdup()


#include<stdio.h>
#include<string.h>
  
int main()
{
    char source[] = "GeeksForGeeks";
  
    // A copy of source is created dynamically
    // and pointer to copy is returned.
    char* target = strdup(source); 
  
    printf("%s", target);
    return 0;
}

Output:

GeeksForGeeks

strndup() :
syntax: char *strndup(const char *s, size_t n);
This function is similar to strdup(), but copies at most n bytes.

We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our
▲ Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/strdup-strdndup-functions-c/ 1/5
10/10/2020 strdup() and strndup() functions in C/C++ - GeeksforGeeks

Note: If s is longer than n, then only n bytes are copied, and a NULL (”) is added at the end.

Below is the C implementation to show the use of strndup() function in C:

// C program to demonstrate strndup()


#include<stdio.h>
#include<string.h>
  
int main()
{
    char source[] = "GeeksForGeeks";
  
    // 5 bytes of source are copied to a new memory
    // allocated dynamically and pointer to copied
    // memory is returned.
    char* target = strndup(source, 5);
  
    printf("%s", target);
    return 0;
}

Output:

Geeks

Reference: Linux man(7)

This article is contributed by MAZHAR IMAM KHAN. If you like GeeksforGeeks and would
like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail
your article to contribute@geeksforgeeks.org. See your article appearing on the
GeeksforGeeks main page and help other Geeks.

Please write comments if you nd anything incorrect, or you want to share more information
about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with
the DSA
We use Self to
cookies Paced
ensure Course at abest
you have the student-friendly price
browsing experience and
on our become
website. industry
By using our ready. Got It !
site, you acknowledge that you have read and understood our
▲ Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/strdup-strdndup-functions-c/ 2/5
10/10/2020 strdup() and strndup() functions in C/C++ - GeeksforGeeks

Recommended Posts:
Write one line functions for strcat() and strcmp()
Functions that are executed before and after main() in C
Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
ll() and ll_n() functions in C++ STL
Forward List in C++ | Set 1 (Introduction and Important Functions)
isalpha() and isdigit() functions in C with cstring examples.
strtok() and strtok_r() functions in C with examples
Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
Ceil and Floor functions in C++
Pure Virtual Functions and Abstract Classes in C++
Wide char and library functions in C++
Find and print duplicate words in std::vector<string> using STL functions
asin() and atan() functions in C/C++ with Example
Explicitly Defaulted and Deleted Functions in C++ 11
atol(), atoll() and atof() functions in C/C++
beta(), betaf() and betal() functions in C++ STL
std::legendre, std::legendref and std::legendrel functions in C++17
asctime() and asctime_s() functions in C with Examples
Static functions in C
Can static functions be virtual in C++?

Improved By to
We use cookies : MattCT,
ensure youriusjeff
have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our
▲ Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/strdup-strdndup-functions-c/ 3/5
10/10/2020 strdup() and strndup() functions in C/C++ - GeeksforGeeks

Article Tags : C C++ C-Library CPP-Library

Practice Tags : C CPP


5

2.5
To-do Done Based on 13 vote(s)

Improve Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

5th Floor, A-118,


 Sector-136, Noida, Uttar Pradesh - 201305
 feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video
We use cookies to ensure you have the best browsing experience on our website. By using Tutorials
our
Got It !
site, you acknowledge that you have read and understood our
▲ Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/strdup-strdndup-functions-c/ 4/5
10/10/2020 strdup() and strndup() functions in C/C++ - GeeksforGeeks

Practice Contribute
Courses Write an Article
Company-wise  Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks , Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our
Got It !
site, you acknowledge that you have read and understood our
▲ Cookie Policy & Privacy Policy

https://www.geeksforgeeks.org/strdup-strdndup-functions-c/ 5/5

You might also like