I'm trying to read compare data from EEPROM (Arduino UNO). Reading is working fine but comparing it using the '==' operator is not working as expected.
//string 'True' is already stored in EEPROM
int addr = 0;
char value = EEPROM.read(addr);
Serial.println(value);// this line successfully prints the letter 'T'
// but this function is not working
if (value == "T") {
Serial.println("foo");
}
-
this has nothing to do with EEPROM ... this is a question about comparing stringsjsotola– jsotola2018年01月08日 18:27:45 +00:00Commented Jan 8, 2018 at 18:27
-
I have a problem like I that did you finally fix it. I will be glad if it worked.Sigma– Sigma2019年09月16日 07:02:26 +00:00Commented Sep 16, 2019 at 7:02
1 Answer 1
There is a HUGE difference between "string"
and 'c' 'h' 'a' 'r'
.
You are comparing address of string "T"
with the numerical value of character 'T'
.
The expression: if (value == 'T')
will be much better.
answered Jan 8, 2018 at 14:17
-
Thank you for the answer. How can I read and compare the first letter of the string stored at addr = 0; ?smc– smc2018年01月08日 14:21:04 +00:00Commented Jan 8, 2018 at 14:21
-
1@ShyamMohan You have to use
'T'
instead of"T"
to compare value with...KIIV– KIIV2018年01月08日 14:23:27 +00:00Commented Jan 8, 2018 at 14:23
lang-cpp