Skip to main content
2 of 4
deleted 5 characters in body
dda
  • 1.6k
  • 1
  • 12
  • 18

char to String conversion in Arduino program

I have started learning about Arduino and am well versed in Java and Python. In an Arduino program, I need a character input via Serial to be stored in a String variable. From my previous programming experience, I knew that a code like the one below would give an error as char can't be converted into String like this.

void loop() {
  if(Serial.available()) {
     String s=(char)Serial.read();
  }
}

But when I defined s as an instance variable and simply assigned its value in void loop(), I got no error.

String s;
void loop() {
  if(Serial.available()) {
     s=(char)Serial.read();
  }
}

What I suspect is that there is some different concept of instance variables here. Can one tell what is going on?