JIYIK CN >

Current Location:Home > Learning > PROGRAM > C++ >

Arduino strcpy function

Author:JIYIK Last Updated:2025/04/16 Views:

In this tutorial, we will discuss strcpy()copying a string from one variable to another using the function in Arduino.


Arduino strcpy()Functions

strcpy()The function can copy a string containing null characters from one variable to another. strcpy()The basic syntax of the function is as follows.

output = strcpy(dest, source);

strcpy()The first input to the function should be of data type char and the second input should be of data type const char. The function returns the copied string as a string.

If the destination size exceeds the source size, strcpy()the function will also add a NUL character as a terminator in the destination string. If the destination string already stores a string, the previous string will be overwritten by the new string.

For example, let us define a string and strcpy()copy it into another variable using the 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 use the Arduino's serial monitor to print the string, which is stored in a destinationvariable. destinationThe length of the variable should be large enough to hold the entire sourcestring.

If the length is smaller than sourcestring, the result will be altered and strcpy()the function will have undefined behavior.

Since sourcethe and destinationstrings are of different sizes, strcpy()the function will overflow, causing problems in your code. The Arduino will not display an error due to overflow, and it may take a while to figure out what the problem is.

For example, let's destinationchange the size of the variable to 5, which is in the above code 17, and then 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 changed because destinationthe size of the variable is less than sourcethe size of the variable. To get good results, we have to make sure that destinationthe size of the variable is greater than sourcethe size of the variable.

We can use strcpy()a length-restricted version of the function, i.e. strncpy()the strncpy()function also sourcecopies the string to destinationthe variable, but it also takes the length of the destination as input.

The function writes NUL characters to fill the remaining space of string only when it sourceencounters a NUL character from string . If string does not have a NUL character, the string will not end with a NUL character.strncpy()destinationsourcedestination

For example, let's strncpy()repeat the above example using the 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 sourcethe first 5 characters of . So, if we use strncpy()the function, we don't have to worry about overflow of the function because strncpy()it will destinationcopy sourcethe number of characters of according to the size of .

This function is also useful when we do not want to copy the entire string but 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.

strcpy()There is another length-restricted version of the function, strlcpy()the function. strlcpy()The function is identical to strncpy()the function, except that strlcpy()the output of the function is the length of the source string.

Unlike strncpy()the function, strlcpy()the function does not write multiple NUL characters to fill destinationthe remaining space of the string, it only writes a single NUL character destinationto the string. destinationThe string will always strlcpy()be terminated with a single NUL character using the function.

destinationThe characters stored in the string will also include the NUL character. For example, if destinationthe size of the string is 5, we can only copy four characters in it due to the NUL character.

So we have to destinationincrease the size of the string to 6 to copy 5 characters into it. However, this is not the case with the strcpy()and strncpy()functions, they add a NUL character only when destinationthe size of the string is greater than sourcethe size of the string.

For example, let's strlcpy()repeat the above example using the 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 string have been copied even though destinationthe size is 5. This is because the function still looks at the end of the string.sourcedestinationstrlcpy()destination

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Stopping a loop in Arduino

Publish Date:2025/04/16 Views:150 Category:C++

This tutorial will discuss the methods to stop a loop in Arduino. There are two types of loops in Arduino: one is the void loop() provided by default and the other is created by the user in it. The loop created by the user can be ended easi

Arduino prints to console

Publish Date:2025/04/16 Views:68 Category:C++

This tutorial will discuss printing text or variables on the console using the Arduino IDE's serial monitor. Arduino using the serial monitor to print to the console The Arduino IDE has a console at the bottom, but we cannot print anything

Arduino Array Length

Publish Date:2025/04/16 Views:181 Category:C++

Arrays are fundamental data structures in programming, and in Arduino, they play a key role when storing and manipulating data. Often, you'll find yourself needing to know the size or length of an array, especially when working on complex p

Arduino 2D Array

Publish Date:2025/04/16 Views:108 Category:C++

In this tutorial, we will discuss about 2D arrays in Arduino. We will discuss how to initialize a 2D array and use it to store data. 2D Array Initialization in Arduino Two-dimensional array initialization is very similar to one-dimensional

Printing Character Array in Arduino

Publish Date:2025/04/16 Views:59 Category:C++

This tutorial will discuss printing character array using loop in Arduino. Serial.println() Define int and print character arrays in Arduino In Arduino, if we int initialize an array using keyword, we have to use a loop to print its element

Arduino Square Wave Generator

Publish Date:2025/04/16 Views:181 Category:C++

digitalWrite() This tutorial will discuss generating a square wave using the function in Arduino . Arduino Square Wave Generator A square wave consists of maximum and minimum values, and the transitions between these values ​​are instan

Splitting a string in Arduino

Publish Date:2025/04/16 Views:188 Category:C++

substring() This tutorial will discuss splitting a string using the function in Arduino . substring() Splitting a string in Arduino using the function Arduino provides an inbuilt function substring() to split a given string. We can split th

Comparing Strings in Arduino

Publish Date:2025/04/16 Views:137 Category:C++

compareTo() This tutorial will discuss comparing two strings using the function in Arduino . compareTo() Comparing strings using the Arduino function To compare two strings in Arduino, we can use compareTo() the function of the string objec

Concatenating strings in Arduino

Publish Date:2025/04/16 Views:190 Category:C++

This tutorial will discuss concat() concatenating two strings using the function or the append operator in Arduino. concat() Concatenate strings using the Arduino function We can concat() concatenate two strings in Arduino using the concate

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial