BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C strncpy() Function – C tutorial

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: c-programming

The strncpy() function is similar to the strcpy() function, except that it copies only the specified number of characters from source string to destination string.

C strncpy() declaration

char *strncpy(char *str1, const char *str2, size_t n)

str1 – Destination string. The string in which the first n characters of source string str2 are copied.
str2 – Source string
n – number of characters of source string that needs to be copied.

Return value of strncpy()

It returns the pointer to the destination string after copying the n characters of source string into it.

Example: strncpy() function

#include <stdio.h>
#include <string.h>
int main () {
   char str1[20]; 
   char str2[25];
   /* The first argument in the function is destination string. 
    * In this case we are doing full copy using the strcpy() function. 
    * Here string str2 is destination string in which we are copying the 
    * specified string 
    */ 
   strcpy(str2, "welcome to beginnersbook.com");

   /* In this case we are doing a limited copy using the strncpy()
    * function. We are copying only the 7 characters of string str2 to str1
    */
   strncpy(str1, str2, 7);

   printf("String str1: %s\n", str1); 
   printf("String str2: %s\n", str2);

   return 0;
}

Output:

String str1: welcome
String str2: welcome to beginnersbook.com

Related Posts:

  1. C – strcoll() function
  2. C – strcmp() function
  3. C – strchr() function
  4. C – strncat() function

Top Related Articles:

  1. C – loops in C programming with examples
  2. C strcoll() Function – C tutorial
  3. C strcmp() Function with example
  4. C strcat() Function with example
  5. C strchr() Function with example

Tags: C-Library, C-String

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap