Strncpy is also known as String Copy Function. It is a crucial function that is offered by the C Language’s C String Library. Strncpy is responsible for copying a chunk of one string’s content into another.
strncpy ( string_a, string_b, 6) will copy the first 6 characters of string “string_b” to the string “string_a”. String_a is the string to which the content of the string is copied. Whereas, string_b is the one that will be used to fetch the first 6 characters of the string or the string from which the first 6 characters are to be copied.
In case the length of the string (from which the part of the string will be copied is less than the string to which the string will be copied), then it will not copy the complete string value to the destination string.
The syntax to call strncpy() function is:
In the syntax above, “char*dest denotes the destination to which the string is to be stored. The second parameter “const char*src” is a standard of C that specifies that the src is a character pointer, and in the third parameter we give the size to our function to define how many characters of the string to copy. The second parameter cannot be modified which is why it is constant. In this function, we store a string in our source parameter and copy it to any destination represented as “dest”. Then, give the size of our destination variable so that the function copies the same number of characters from our string x. We can also pass NULL to the source parameter.
Example # 1:
In this example, we have declared two-character arrays that are X and Y using an array of size 20 for “x” and 5 for “Y”. We have stored String “Hello World” in our X variable which is our source variable.
After declaring our character arrays, we will call our strncpy function to copy our desired string to the other. In strncpy, we passed both arrays destination as well as the source array along with the size to which the string is copied.
In the above code, Y is our destination character string which we discussed earlier in the introduction. This function has three parameters: source, destination, and length. Our main objective is to copy the string value which we have assigned to our source array “x” into our destination array “y”. We have given the length of the Y variable as the third parameter so that it copies the same number of characters as that of the length of Y. Using the printf() statement, we will print the copied array as output where %s is used to print the string from the first character to the null “\0” character and “y” is the resulting array that has to be printed.
Our code will give the following output once it is executed. As we can see in our output, it has not copied the whole phrase because we gave it the length of Y which was just 5 so it only copied 5 characters from the string.
Example # 2:
In the following example, we first initialized our char arrays named “a” and “b” to a fixed size of 6. After initialization, we passed our arrays to the strncpy function with the length of the string to which it will be copied.
When strncpy copies the content of the source to the destination, it copies the string without a terminating the NULL character. Here, we are specifying the size of parameter b. The whole string will be copied to “b”. Strncpy never adds a \0 or NULL character at the end when the source size is equal to the destination size, so we have to manually add the \0 or NULL character at the end of our character “b”.
If we take a look at our output, we have copied our string value from one character to another. That is why we have our output as “Great”.
Example # 3:
In the code sample below, we are using the method of getting the array copied to a constant specified value. To do so, we will first declare our two string variables using a character array to a specified length: “stringA” with a length of 20 and “stringB” with a length of size 25. We haven’t assigned any string to our source array using method strcpy. We will insert the string manually to the source variable “stringB”. Then, call our strncpy function in which the first argument is the destination variable and the second is a source variable. But we are assigning the value to our source also by using our strncpy function.
In line 6, we are doing a full copy of our string to assign the value to our source. After we have assigned the value, we copied the StringB value to StringA which is our destination. We have given it a length of 7 which means it will only copy the first 7 characters of our source string.
We used printf() statement, one to print our source value and the other to print our destination value. As we can see in our output, the StringA value is only 7 characters because while copying, we gave it a length of 7. stringB printed the full string because we used strcpy instead of strncpy function which copied the whole string.
In this guide, we have learned about the strncpy() function of the C language. You can explore more about the strncpy() function of the C library by using multiple examples, you can gain an even better understanding by implementing it. It enabled us to use a single string in a code multiple times instead of writing it again and again. By just assigning it to one variable reduces the complexity of repeating values.