What is strcpy in c programming?

strcpy function is used to copy the string from source to destination.

Whenever strcpy encounters null character it stops copying.

strcpy function is defined in string.h header file.

strcpy function returns a pointer to the destination string.

Prototype

char *strcpy(char *destination, const char *source);

Parameters

strcpy function takes destination pointer and source pointer.

Return value

strcpy function returns a pointer to the destination string.


Example program for strcpy

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

Code explanation

The source string is being copied to the destination string after calling strcpy function.

The result is printed on to the console.

	The destination string is scholarsoul 
strcpy in c programming

People also ask for

What is strcpy?

strcpy function is used to copy the string from source to destination. This function is defined in string.h header file.

How does Strcpy work in C?

Strcypy copies source string to destination string. It begins from first character of source string and copies each character one by one.

What library is Strcpy in C?

strcpy function is found in string.h header file of c standard library.

Does Strcpy copy null character C?

It does not copies a null character. When it encounters null character then it stops copying.

Why is Strcpy bad?

strcpy is bad because it may cause buffer overflow as it checks null character for stop copying.

Is Strcpy safe?

strcpy is not safe because it doesnot specify any size, it may cause buffer overflow.

Does Strcpy allocate memory?

strcpy doesnot allocate memory on its own.

What does Strlcpy return?

strcpy in c returns a pointer to the destination string.

Do you need to malloc before Strcpy?

It is not mandatory to use malloc() function before strcpy() function. We have to allocate memory for string we can also use static allocation of character array.

What can I use instead of Strcpy?

You can make use of strncpy() function as it does not have the problem of buffer overflow.


People also read


Leave a Reply