strcpy strncpy in c example illustration


Advertisements

Share

Description
    The C library function char *strcpy(char *dest, const char *src) and char *strncpy(char *dest, const char *src, size_t n) are used to copy characters from src string to dest string.
    strcpy() function copies the source string pointed to by src, including the terminating null character ('\0'), to the destination string pointed to by dest. The destination string dest must be large enough to hold the entire content of source string including null characters.
    strncpy() function copies upto n bytes of source string src to destination string dest. strncpy does not terminate the destination string dest with null character. Also if length of the source string is less than n characters, remaining no of charatcers will be padded as null bytes in the destination string.


strcpy, strncpy header file
    The functions strcpy and strncpy are included in the C standard library header file string.h.


strcpy, strncpy function declaration, arguments
    The function strcpy is declared as the following.

            char *strcpy(char *dest, const char *src);
        
src  This is a pointer to a source string.
dest  This is a pointer to a destination string.
    The function strncpy is declared as the following.

            char *strncpy(char *dest, const char *src, size_t n);
        
src  This is a pointer to a source string.
dest  This is a pointer to a destination string.
n  n is the number of characters to be copied from src string to destination string.

strcpy, strncpy function return value
    Both strcpy and strncpy functions returns a pointer to the destination string dest.

strcpy Illustration examples

Listing1.c: Program to illustrate the usage of strcpy library function.

#include <stdio.h>
#include <string.h>
int main() {
  char src[] = "source";
  char dest[7]; /* Should be at least 7 bytes to hold src */
  strcpy(dest, src);
  printf("dest is %s\n",dest);
  return 0;
}

Listing1.c program output.

$ ./a.out
dest is source
$


Listing1.c program analysis
    In Listing1.c program dest string length should be atleast the length of src string including null character(source) which is 7. If destination string dest is not large enough to hold the characters of source string src, then there will be buffer overruns which can create uppredictable behaviour.
strncpy Illustration examples

Listing2.c: Program to illustrate the usage of strncpy library function.

#include <stdio.h>
#include <string.h>
int main() {
  char strs[] = "Bangalore-2134";
  char strd[10];
  strncpy(strd, strs, 9);
  printf("strd is %s\n",strd);
  return 0;
}

Listing2.c program output.

$ ./a.out
strd is Bangalore
$


Listing2.c program analysis
    In Listing2.c program we have copied strs string "Bangalore-2134" which is more than 9 bytes. But strncpy copies only 9 charatcers to strd. Hence we get Bangalore as destination string. But the program will not produce Bangalore as ouput as Bangalore as strncpy does not terminate the strd string with '\0' character after copying to it. So instead of using strncpy it is recommended to start with an empty string and then use strncat to concatenate the required number of characters to the destination string. This is illustrated below.
Listing3.c: Program to illustrate the usage of strncat library function instead of strncpy.

#include <stdio.h>
#include <string.h>
int main() {
  char strs[] = "Bangalore-2134";
  char strd[10];
  strd[0] = '\0';
  strncat(strd, strs, 9);
  printf("strd is %s\n",strd);
  return 0;
}

Listing3.c program output.

$ ./a.out
strd is Bangalore
$


Listing3.c program analysis
    In Listing3.c program we are getting the same output as Listing2.c, but it is confirmed that the destination string has a null character after copying the n characters from source string. Hence it will always produce same output Bangalore.


Want to contribute or ask a new article? Write and upload your article information .

Share

Articles


C Programming

More Articles Categories

Technical articles

Prepare for software jobs


Test your skills