In this tutorial, we will discuss how to use the strncpy() function in the C programming language. The strncpy function in C is used to copy specified bytes of characters from a source to a specified destination. It is defined in the string.h header file which need to be included before using the function.

Basic Usage

The strncpy() function is similar to the strcpy() function. However, unlike strcpy(), it copies a specified number of characters from the source string to the destination string.

The strncpy() syntax is as:

char* strncpy(char *dest, const char* src, size_t n);

Function Parameters

The strncpy() function accepts 3 parameters:

  1. dest – This is a pointer to a character array where the set characters are stored after copy.
  2. src – This pointer points to the character array from where the specified character arrays are copied.
  3. n – the maximum number of characters to copy from the src string.

The function copies the number of characters specified by n from the src to the memory pointed by dest.

If the function receives the value of n less than the length of the src array, it copies the specified n characters without a null terminating character.

If n is more than the length of the src array, it copies all the n characters with a null-terminated character to dest.

Function Return Value

The function returns a pointer to the dest array.

Example 1

The following example illustrates how to use the strncpy() function.

#include

#include

int main() {


    char src[] = “Hello, world!”;


    char dest[20];

    strncpy(dest, src, sizeof(dest));


    printf(“Copied string: %s n, dest);

    return 0;

}

In the above example program, we specify the source string with the value: “Hello, world!”. We then proceed to define the destination string and use the strncpy() function to copy the size of the source string.

The output from the above program is as shown:

Copied string: Hello, world!

Example 2

The essence of the next example is to illustrate how to copy partial characters from the source string. For example, to copy the first 5 characters, we can do:

#include

#include

int main() {


    char src[] = “Hello, world!”;


    char dest[20];

    strncpy(dest, src, 5);


    printf(“Partial string: %s n, dest);

    return 0;

In the example program above, we only copy 5 bytes from the source string. Since the value of n is less than the length of the source string. The destination string is not null terminated.

Example 3

We can add a null terminating character manually as shown in the example program below:

#include

#include

int main() {


    char src[] = “Hello, world”;


    char dest[10];

    strncpy(dest, src, 5);


    dest[5] = ;


    return 0;

}

In the example above, we manually add a null terminating character after copying the target number of bytes from the source string

Example 4

If you pass the value of n greater than the size of the source string, the strncpy() function copies all the characters in from the source string and adds a series of null-terminating characters for the remaining bytes.

#include

#include

int main() {


    char src[] = “Hello, world”;


    char dest[20];


    strncpy(dest, src, sizeof(src) 5);

    printf(“Destination %s n, dest);


    return 0;

}

Conclusion

In this tutorial, we discussed how to use the strncpy() function in C. Using this function, you can copy specific number of characters from a string to a destination string.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/john-150×150.png6126590ae4d52.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list