You seem to be under the completely mistaken impression that + is the concatenation operator in C. It is the ADDITION operator. Grab your calculator. ADD "p" and 12. What is the result?
PaulS:
You seem to be under the completely mistaken impression that + is the concatenation operator in C. It is the ADDITION operator. Grab your calculator. ADD "p" and 12. What is the result?
"p" is a char, not a String (with uppercase S), + operates on char much like two values in a calculator.
However,
void setup() {
Serial.begin(9600);
}
void loop() {
String p = "p";
int i = 12;
String c = p + i + p;
Serial.println(c);
}
is String addition (concatenation) that does work.
One can add the value of a function to a String, as long as the String has been initialized beforehand.
One should concatenate Strings on a line before using Serial.print().
See 08. Strings for more information.
You could look at using the PrintEx library.
That will add printf() support to Print class objects - which eliminates having to use sprintf() to buffer each time you want to output a xxprintf() formatted string.
But as long it's a print statement, don't bother concating it first, just send it in pieces, nobody will know. In case of the web with a .get(), yeah, you unfortunately have to.
septillion:
Why o why. Just print each part and don't spend time to concat them in memory when you don't need it..
I'm a php programmer with an Arduino hobby.
C++ is total nuts.
I just want a forum that helps with total 1980 wackyness of C.
I need to make a filename for my SD reader.
And I'm searching this idiotic forum for help.
Look how other forums help:
search in google: create string with variables php
Need to waist 4 hours of time? Then try to get the same answer for Arduino on this forum.
After reading 9127 comments of idiots like Paul_S I ended up using this:
#include <stdio.h>
unsigned int selectedYear;
char filename[14] = {}; // ROMEO filenames are 8.3 so 12 + 1 for END-OF-STRING should be enough...
void createFilename(int yr) {
sprintf(filename, "thumb%u.bmp", yr);
}
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
selectedYear = 1970;
Serial.println("Start");
}
// the loop routine runs over and over again forever:
void loop() {
createFilename(selectedYear);
Serial.print("- ");
Serial.println(filename);
selectedYear++;
if ( selectedYear == 2021 ) selectedYear = 1970;
delay(200);
}
Try to write real time stuff in PHP, Java or C#,even on the fastest PC that you can find in the world; and I mean real time stuff where microseconds can cost you thousands of dollars. You will fail miserably. Those languages are basically only good for user interfaces.