How do I print a string and integer on an LCD? Here is what I have tried:

int number = 2;
int result = (number + number);
lcd.print(number, DEC + " plus " + number, DEC + " equals " + result, DEC);

And the result is an error. When I try:

lcd.print(number + "look");


lcd.print("1234 " + number);

Then the result on the LCD is "34 ", as it deleted the first two numbers!

I want it to print:

1234 2

Please help! Thanks!


The lcd.print() method takes a string or a number as an argument, but not both at once. Using the + operator for concatenation is not native to C strings. C strings are basically stored as pointers to the first character of the string. So when you tried lcd.print("1234 " + number) and got "34 ", what most likely happened is that you advanced the pointer, pointing to the first character of "1234 "(i.e. '1') by number (i.e. 2), thus making the pointer point to the 3rd character in the string and thus making lcd.print() display "34 ". In order to print something that contains both strings and integers, the most straightforward way is to convert each piece to an Arduino string and then concatenate them, using the + operator like you did earlier.

    lcd.print(String("1234 ") + String(number));
    lcd.print(String(number) + String(" plus ") + String(number) + String(" equals ") + String(result));

These are some examples. You could also use create a sufficiently large char buffer and use sprintf() from stdio.h to build a string made up of integers and strings. Look up a textbook on C/C++ for more details.

| improve this answer | |

I got the same problem and I printed the string and the integer on the same line by moving the cursor like this :

   lcd.setCursor(0, 0);    <----------Column 0 , Line 1
   lcd.print("This code failed");
   lcd.setCursor(0, 1);    <----------Column 0 , Line 2
   lcd.print("Error Code = ");
   lcd.setCursor(13, 1); <-change the cursor for the INT. Column 13 , Line 2
| improve this answer | |

What is wrong with this:

lcd.print(number, DEC + " plus " + number, DEC + " equals " + result, DEC);

It is the number of parameters and the assumption that plus will concatenate string literals.

lcd.print("1234 " + number);

This gives "34 " when number is 2. Why? This is because that is the result of adding 2 to the string literal pointer, i.e. the string starting at the character "3". Please use separate print statements instead:

lcd.print("1234 ");

Do not use String to concatenate strings. That is a waste of SRAM, processing and has a risk of heap fragmentation. Simply a bad idea in small scale embedded systems such as AVR.

To understand these errors you should go back and read about strings and string literals in C/C++ (compared to Java :).


| improve this answer | |
int a=0;
float b=10.01;

char str[16];

sprintf(str,"a=%d, b=%.2f, c=%d", a, b, 2); // use sprintf() to compose the string str


The output will be 'a=0, b=10.01, c=2'

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.