Home > Projects > How To – DIY – Projects > Arduino String Addition Operator Code

Arduino String Addition Operator Code

Summary of Arduino String Addition Operator Code


This article explains String concatenation in Arduino using the + operator. It details combining Strings with constants, variables, characters, arrays, and function results like millis() or analogRead(). The text emphasizes that proper initialization is critical to avoid unpredictable results or compilation errors when mixing data types on a single line.

Parts used in String Concatenation Project:

  • String variable
  • Integer constant
  • Long integer constant
  • Character constant
  • String constant
  • Function returning long integer (millis)
  • Function returning integer (analogRead)

You can add Strings together in a variety of ways. This is called concatenation and it results in the original String being longer by the length of the String or character array with which you concatenate it. The + operator allows you to combine a String with another String, with a constant character array, an ASCII representation of a constant or variable number, or a constant character.

 Arduino String Addition Operator
// adding a constant integer to a string:
stringThree =  stringOne + 123;

// adding a constant long interger to a string:
stringThree = stringOne + 123456789;

// adding a constant character to a string:
stringThree =  stringOne + ‘A’;

// adding a constant string to a string:
stringThree =  stringOne +  “abc”;

// adding two Strings together:
stringThree = stringOne + stringTwo;

You can also use the + operator to add the results of a function to a String, if the function returns one of the allowed data types mentioned above. For example,

stringThree = stringOne + millis();

This is allowable since the millis() function returns a long integer, which can be added to a String. You could also do this:

stringThree = stringOne + analogRead(A0);

because analogRead() returns an integer. String concatenation can be very useful when you need to display a combination of values and the descriptions of those values into one String to display via serial communication, on an LCD display, over an Ethernet connection, or anywhere that Strings are useful.

Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

int sensorValue = analogRead(A0);
String stringOne = “Sensor value: “;
String stringThree = stringOne + sensorValue;
Serial.println(stringThree);

results in “Sensor Value: 402” or whatever the analogRead() result is, but

int sensorValue = analogRead(A0);
String stringThree = “Sensor value: ” + sensorValue;
Serial.println(stringThree);

gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.

Here’s another example where improper initialization will cause errors:

Serial.println(“I want ” + analogRead(A0) + ” donuts”);

This won’t compile because the compiler doesn’t handle the operator precedence correctly. On the other hand, the following will compile, but it won’t run as expected:

For more detail: Arduino String Addition Operator Code

Quick Solutions to Questions related to String Concatenation Project:

  • What is string concatenation?
    It is adding strings together which results in the original string being longer by the length of the added string or character array.
  • How can you combine a string with other data types?
    You can use the plus operator to combine a string with another string, a constant character array, an ASCII representation of a number, or a constant character.
  • Can you add function results to a string?
    Yes, you can add the results of a function to a string if the function returns an allowed data type like a long integer or an integer.
  • Why is string concatenation useful?
    It is useful for displaying a combination of values and their descriptions into one string for serial communication, LCD displays, or Ethernet connections.
  • What happens if you concatenate multiple variable types without initializing them?
    You may get unpredictable results because the variable never got an initial value before starting to concatenate different data types.
  • Why might a code snippet fail to compile regarding operator precedence?
    The compiler might not handle operator precedence correctly if you try to print a literal string combined directly with a function call in a specific order.
  • Does the millis function return a type compatible with string addition?
    Yes, millis returns a long integer which can be added to a string.
  • Is analogRead compatible with string addition?
    Yes, analogRead returns an integer which is an allowed data type for string concatenation.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top