strncpy in c

What is strncpy in c programming?

strncpy function copies n characters from source to destination.

This function is defined in string.h header file.

This function returns a copied string.

Prototype

char *strncpy(char *destination , const char *source, size_t m);

Parameters

strncpy function takes source string, destination pointer and size value as input parameters.

Return value

strncpy function returns a copied string.


Example program for strncpy

#include<stdio.h>
#include<string.h>
int main()
{
  char source[30]="scholarsoul";
  char dest[30];
  strncpy(dest,source,05);
  printf("The destination string is %s \n",dest);
  return 0;
}

Output

	The destination string is scholar

Code explanation

strncpy function is called by passing parameters like source string, destination string where source string is to be copied and length attribute.

Copied string is printed on to the console.


People also ask for

What is strncpy function?

strncpy function copies upto n characters from source to destination. This function is defined in string.h header file.

What is difference between strcpy and strncpy?

strcpy() function copies whole string and strncpy() function copies number of characters specified.


People also read


Leave a Comment