Thursday 12 January 2012

Strncpy Function

Synopsis:
#include <stdio.h>
char *strncpy (char *dest, char *src, int n);
Description:
The strcpy function copies n characters from src to dest up to and including the terminating null character if length of src is less than n.
Return Value
The strncpy function returns dest.
Example
#include <stdio.h>
int main()
{
  char input_str[20];
  char *output_str;
  strncpy(input_str, "Hello", 20);
  printf("input_str: %s\n", input_str);
  /* Reset string */
  memset(input_str, '\0', sizeof( input_str ));
  strncpy(input_str, "Hello", 2);
  printf("input_str: %s\n", input_str);
  /* Reset string */
  memset(input_str, '\0', sizeof( input_str ));
  output_str = strncpy(input_str, "World", 3);
  printf("input_str: %s\n", input_str);
  printf("output_str: %s\n", output_str);
  return 0;
}
It will proiduce following result:
input_str: Hello
input_str: He
input_str: Wor
output_str: Wor

No comments:

Post a Comment

host gator coupon