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:
Note: strncpy() function not copy the 'NULL character'( i.e. '\0').
Related program:
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:
Figure: Screen shot for illustrate strncpy() function C program |
Note: strncpy() function not copy the 'NULL character'( i.e. '\0').
Related program:
- List of standard library function
- strlen()
- strcpy()
Comments
Post a Comment