C program strcpy




















C strcpy In this tutorial, you will learn to use the strcpy function in C programming to copy strings with the help of an example. The strcpy function also returns the copied string. The strcpy function is defined in the string. Share on:. Did you find this article helpful? Sorry about that. How can we improve it? Leave this field blank. Related Library Functions. C Library Function C strcmp. C Library Function C strcat. C Library Function C strcpy. C Library Function C strlen.

Get App Get C App. Notice that source is preceded by the const modifier because strcpy function is not allowed to change the source string. It is important to note that strcpy function do not check whether the destination has enough size to store all the characters present in the source. It is the responsibility of the program to make sure that the destination array has enough space to accommodate all the characters of the source string. Another important point to note about strcpy is that you should never pass string literals as a first argument.

For example:. Since modifying a string literal causes undefined behaviour, calling strcpy in this way may cause the program to crash. In line 18, we have assigned the base address of the destination to start , this is necessary otherwise we will lose track of the address of the beginning of the string. In line 20, we have while loop, the while loops copies character from source to destination one by one.



0コメント

  • 1000 / 1000