Arduino strcpy Function

In this tutorial, we will discuss copying one string from one variable to another using the strcpy()
function in Arduino.
Arduino strcpy()
Function
The strcpy()
function can copy a string including the null character from one variable to another. The basic syntax of the strcpy()
function is shown below.
output = strcpy(dest, source);
The first input of the strcpy()
function should have the data type char, and the second input should be the data type const char. This function will return the copied string as a character string.
If the destination size exceeds the source size, the strcpy()
function will also add a NUL character as a terminator inside the destination string. If the destination string already has a string stored, the previous string will be overwritten by the new string.
For example, let’s define a string and copy it into another variable using the strcpy()
function. See the code below.
void setup() {
const char* source = "Hello World";
char destination[17];
Serial.begin(9600);
strcpy(destination, source);
Serial.println(destination);
}
void loop() {
}
Output:
Hello World
In the above code, we used the serial monitor of Arduino to print the string, which is stored inside the destination
variable. The length of the destination
variable should be large enough to hold the entire source
string.
If the length is less than the source
string, the result will be changed, and the strcpy()
function will have undefined behavior.
Because of the difference in the size of the source
and destination
strings, the strcpy()
function will overflow, which will cause problems in the code. Arduino will not show an error because of overflow, and it might take a while to figure out the problem.
For example, let’s change the size of the destination
variable to 5
, which is 17
in the above code, and check the result. See the code below.
void setup() {
const char* source = "Hello World";
char destination[5];
Serial.begin(9600);
strcpy(destination , source);
Serial.println(destination);
}
void loop() {
}
Output:
Èõüÿ
As we can see in the above output, the result has been changed because the size of the destination
variable is less than that of the source
variable. For a good result, we must ensure the destination
variable’s size is greater than the source
variable’s size.
We can use the length limited version of the strcpy()
function which is strncpy()
. The strncpy()
function also copies the source
string to the destination
variable, but it also takes the length of the destination as input.
The strncpy()
function will write NUL characters to fill the remaining space of the destination
string only if a NUL character is encountered from the source
string. If the source
string does not have a NUL character, the destination
string will not be terminated with a NUL character.
For example, let’s repeat the above example using the strncpy()
function. See the code below.
void setup() {
const char* source = "Hello World";
char destination[5];
Serial.begin(9600);
strncpy(destination , source, sizeof(destination));
Serial.println(destination);
}
void loop() {
}
Output:
Hello
The output of this example contains the first 5 characters of the source
. So, if we use the strncpy()
function, we don’t have to care about the overflow of the function because strncpy()
will copy the number of characters of the source
according to the destination
size.
This function is also useful when we don’t want to copy the whole string and only want to copy a few characters from the source to the destination. The strcpy()
and strncpy()
functions also return the copied string of char data type.
There is also another length limited version of the strcpy()
function which is strlcpy()
function. The strlcpy()
function is the same as the strncpy()
function and the difference is that, the output of the strlcpy()
function is the length of the source string.
Unlike the strncpy()
function, the strlcpy()
function does not write multiple NUL characters to fill the remaining space of the destination
string, and it only writes a single NUL character to the destination
string. The destination
string will always be terminated with a single NUL character using the strlcpy()
function.
The characters stored inside the destination
string will also include the NUL character. For example, if the size of the destination
string is 5, we can only copy four characters in it because of the NUL character.
So we have to increase the size of the destination
string to 6 to copy 5 characters in it. However, this is not the case with the strcpy()
and the strncpy()
functions which only add a NUL character if the size of the destination
string is greater than the size of the source
string.
For example, let’s repeat the above example using the strlcpy()
function. See the code below.
void setup() {
const char* source = "Hello World";
char destination[5];
Serial.begin(9600);
strlcpy(destination , source, sizeof(destination));
Serial.println(destination);
}
void loop() {
}
Output:
Hell
As we can see in the output, four characters of the source
string have been copied to the destination
even if the destination
size is 5. This is because the strlcpy()
function also added the NUL character at the end of the destination
string.
Related Article - Arduino String
- Arduino strcmp Function
- Concatenate Strings in Arduino
- Parse a String in Arduino
- Split String in Arduino
- Compare Strings in Arduino