

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
strdup() and strdndup() in C/C++
strdup()
The function strdup() is used to duplicate a string. It returns a pointer to null-terminated byte string.
Here is the syntax of strdup() in C language,
char *strdup(const char *string);
Here is an example of strdup() in C language,
Example
#include <stdio.h> #include<string.h> int main() { char *str = "Helloworld"; char *result; result = strdup(str); printf("The string : %s", result); return 0; }
Output
The string : Helloworld
strndup()
The function strndup works similar to the function strndup(). This function duplicates the string at most size bytes i.e. the given size in the function. It also returns a pointer to null-terminated byte string.
Here is the syntax of strndup() in C language,
char *strndup(const char *string , size_t size);
Here is an example of strndup() in C language,
Example
#include <stdio.h> #include<string.h> int main() { char *str = "Helloworld"; char *result; result = strndup(str, 3); printf("The string : %s", result); return 0; }
Output
The string : Hel
- Related Questions & Answers
- INT_MAX and INT_MIN in C/C++ and Applications
- Foreach in C++ and C#
- Comma in C and C++
- Loops in C and C++
- isalpha() and isdigit() in C/C++
- nextafter() and nexttoward() in C/C++
- Undefined Behaviour in C and C++
- rand() and srand() in C/C++
- # and ## Operators in C ?
- Floating Point Operations and Associativity in C, C++ and Java
- exit(), abort() and assert() in C/C++
- Difference between Structures in C and C++
- Alternating Vowels and Consonants in C/C++
- Variable Length Arrays in C and C++
- Covariance and Contravariance in C#
Advertisements