You are on page 1of 11

Skip to content

 Tutorials
 Student
 Jobs
 Courses

Write
Come write articles for us and get featured

Practice
Learn and code with the best industry experts

Premium
Get access to ad-free content, doubt assistance and more!

Jobs
Come and find your dream job with us

o Geeks Digest
o Quizzes
o Geeks Campus
o Gblog Articles
o IDE
o Campus Mantri



 My Profile  Edit Profile

 My Courses  Go Premium

  Logout
 Home
 Courses

o Practice DS & Algo.
o Algorithms
o Analysis of Algorithms
o Data Structures
o Interview Corner
o Languages
o ISRO CS
o GATE
o CS Subjects
o Web Technologies
o School Learning
o Mathematics
o Maths Notes (Class 8-11)
o NCERT Solutions
o RD Sharma Solutions
o UGC NET CS
o Student
o Jobs
 GBlog
 Puzzles
 What's New ?
 Change Language

Related Articles

Related Articles
 strdup() and strndup() functions in C/C++
 How to pass an array by value in C ?
 Reverse words in a given string
 Print words of a string in reverse order
 Different methods to reverse a string in C/C++
 std::reverse() in C++
 How to reverse a Vector using STL in C++?
 What are the default values of static variables in C?
 Understanding “volatile” qualifier in C | Set 2 (Examples)
 Const Qualifier in C
 Initialization of static variables in C
 Understanding “register” keyword in C
 Understanding “extern” keyword in C
 Storage Classes in C
 Static Variables in C
 Memory Layout of C Programs
 How to deallocate memory without using free() in C?
 Difference Between malloc() and calloc() with Examples
 Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
 How to dynamically allocate a 2D array in C?
 How to pass a 2D array as a parameter in C?
 Multidimensional Arrays in C / C++
 2D Vector In C++ With User Defined Size
 Vector of Vectors in C++ STL with Examples
 Vector in C++ STL
 What is Memory Leak? How can we avoid?
 std::sort() in C++ STL
 Arrays in C/C++
 Bitwise Operators in C/C++

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


 Difficulty Level : Medium
 Last Updated : 10 Jun, 2021
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(). 
It returns a pointer to the duplicated string s.
Below is the C implementation to show the use of strdup() function in C:
 
 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. 
Note: If s is longer than n, then only n bytes are copied, and a NULL (‘\0’) is added at
the end.
Below is the C implementation to show the use of strndup() function in C:
 
 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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org.
See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
 
Want to learn from the best curated videos and practice problems, check out the C
Foundation Course for Basic to Advanced C.

Like11
Next
How to pass an array by value in C ?
ADVERTISEMENT BY ADRECOVER

RECOMMENDED ARTICLES
Page :

1
2
3

Write one line functions for strcat() and strcmp()

31, May 10

Functions that are executed before and after main() in C

30, Sep 11

fill() and fill_n() functions in C++ STL

31, Mar 16

isalpha() and isdigit() functions in C with cstring examples.

26, Nov 16

strtok() and strtok_r() functions in C with examples

24, Dec 16

Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)

23, Apr 17

Ceil and Floor functions in C++


04, May 17

Pure Virtual Functions and Abstract Classes in C++

15, Jul 14

Wide char and library functions in C++


21, May 17

Find and print duplicate words in std::vector<string> using STL functions


23, Jun 17

asin() and atan() functions in C/C++ with Example


09, Apr 18

Explicitly Defaulted and Deleted Functions in C++ 11


08, May 18

atol(), atoll() and atof() functions in C/C++


21, Jun 18

beta(), betaf() and betal() functions in C++ STL


02, Jul 18
std::legendre, std::legendref and std::legendrel functions in C++17
24, Jul 18

asctime() and asctime_s() functions in C with Examples


19, Aug 19

Forward List in C++ | Set 1 (Introduction and Important Functions)


12, Jul 16

Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)


06, Jan 14

Static functions in C
05, May 10

Can static functions be virtual in C++?


18, Aug 10

Virtual functions in derived classes


30, Sep 10

Functions that cannot be overloaded in C++


18, Feb 11
Some interesting facts about static member functions in C++
09, Jan 11

Pure Functions
10, Dec 11

Article Contributed By :

GeeksforGeeks
Vote for difficulty

Current difficulty : Medium


EasyNormalMediumHardExpert

Improved By :

 MattCT
 riusjeff
 koushikpmv
Article Tags :

 C-Library
 CPP-Library
 C Language
 C++
Practice Tags :

 CPP
Improve Article

Report Issue

ADS BY ADRECOVER

WHAT'S NEW
Ad free experience with GeeksforGeeks Premium

View Details

DSA Self Paced Course

View Details

DSA Live Classes for Working Professionals

View Details

ADS BY ADRECOVER

MOST POPULAR IN C LANGUAGE


 Converting Strings to Numbers in C/C++
 Core Dump (Segmentation fault) in C/C++
 Left Shift and Right Shift Operators in C/C++
 Substring in C++
 Structures in C
ADS BY ADRECOVER

MOST VISITED IN C++


 The C++ Standard Template Library (STL)
 Map in C++ Standard Template Library (STL)
 Object Oriented Programming in C++
 Inheritance in C++
 Initialize a vector in C++ (6 different ways)
ADS BY ADRECOVER
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

ADVERTISEMENT BY ADRECOVER

5th Floor, A-118,


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

 Company
 About Us
 Careers
 Privacy Policy
 Contact Us
 Copyright Policy
 Learn
 Algorithms
 Data Structures
 Languages
 CS Subjects
 Video Tutorials
 Practice
 Courses
 Company-wise
 Topic-wise
 How to begin?
 Contribute
 Write an Article
 Write Interview Experience
 Internships
 Videos
@geeksforgeeks , Some rights reserved

You might also like