What is the difference between strcpy() and strncpy() functions in C?

C Programming Language >   String in C Language >   Introduction to String  

Short Question

18505


Answer:

  • strcpy( ) function copies whole content of one string into another string. Whereas, strncpy( ) function copies portion of contents of one string into another string.
  • If destination string length is less than source string, entire/specified source string value won’t be copied into destination string in both cases.

Here's a comparison between the strcpy() and strncpy() functions in C:

Aspect strcpy() strncpy()
Function Copies a string from source to destination. Copies a specified number of characters from source to destination.
Signature char* strcpy(char* destination, const char* source); char* strncpy(char* destination, const char* source, size_t num);
Null Terminator Always adds a null terminator at the end of the copied string. Adds null terminator only if the number of characters copied is less than the specified length num.
Buffer Safety No built-in mechanism to prevent buffer overflow. Provides some buffer safety by specifying the maximum number of characters to copy.
Length Copies the entire string until the null terminator is found. Allows specifying the maximum number of characters to copy, which can be smaller or larger than the source string length.
Return Value Returns a pointer to the destination string. Returns a pointer to the destination string.

In summary, the main differences between strcpy() and strncpy() are that strcpy() copies the entire string until the null terminator, whereas strncpy() allows specifying the maximum number of characters to copy. Additionally, strcpy() always adds a null terminator, while strncpy() only adds a null terminator if the number of characters copied is less than the specified length. However, it's important to note that strncpy() does not automatically null-terminate the destination string if the specified length is equal to or greater than the length of the source string. Care should be taken to handle null termination correctly when using strncpy().

The strcpy() function in C is used to copy a string from one character array (source) to another (destination). It stands for "string copy." Here is the general syntax of the strcpy() function:


char* strcpy(char* destination, const char* source);

Parameters:

  • destination: A pointer to the destination character array where the string will be copied.
  • source: A pointer to the source character array, which contains the string to be copied.

Return Value:

  • The strcpy() function returns a pointer to the destination character array (destination).

Example Usage:


#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    strcpy(destination, source);

    printf("Copied string: %s\n", destination);

    return 0;
}

In the above example, the strcpy() function is used to copy the contents of the source array to the destination array. After the strcpy() function is called, the destination array will contain the same string as the source array, "Hello, World!". The resulting string is then printed using printf().

It's important to note that when using strcpy(), you need to ensure that the destination array is large enough to accommodate the copied string to avoid buffer overflow and memory corruption.


This Particular section is dedicated to Question & Answer only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.




Join Our telegram group to ask Questions

Click below button to join our groups.