Sunday, 28 April 2013

strncpy()

This function copy only first n characters of source string to target string.
n = no. of characters(i.e. numeric value)

syntax:

strncpy("target_string","Source_string",no_of_characters);

example:

strncpy(target,source,3);
Output: souget

illustrate uses of strncpy() in C program:


/*c program to illustrate strncpy() function*/
#include<stdio.h>
int main()
{
 char source[40],target[40]="target";
 printf("Enter any string : ");
 gets(source);
 strncpy(target,source,3);
 printf("\nTarget:%s \nSource:%s",target,source);
 getch();
 return 0;
}

The output of above program would be:


Output of strncpy() function uses in C program
Figure: Screen shot for illustrate strncpy() function C program


Note: strncpy() function not copy the 'NULL character'( i.e. '\0').


Related program:
  1. List of standard library function
  2. strlen()
  3. strcpy()

No comments:

Post a Comment