Short minimal program: If equal #define number, print not happening

#define head 1

char charArr1[130];

void setup()
{
  Serial.begin(19200);
  delay(1000);

  charArr1[0] = '1';
  
  char motHead[1];
  itoa(head, motHead, 10);
  if(charArr1[0] == motHead)
    Serial.println("Works");
}

void loop(){}

Is there a way to check the first value of the character array against an integer? Any suggestions on how to get itoa to work or something else?

Is there a way to check the first value of the character array against an integer?

well first of all a character is a 'byte' (signed at that) And i think in this case using a define is clumsy (or it's gonna look clumsy)
If you declare this array like this charArr1[0] = '1';that means that it will actually be a character or if considered a 'byte' it will have value '49' Even though you question is quite elaborate, i am not exactly sure of what you want. a simpleconst char head = '1'; and you would be able to compare the characters as you please. But of course this only works for single digits.

I suppose '1' is good enough:

#define head '1'

char charArr1[130];

void setup()
{
  Serial.begin(19200);
  delay(1000);

  charArr1[0] = '1';
  
  if(charArr1[0] == head)
    Serial.println("Works");
}

void loop(){}

Thanks.

If you want to use 1 instead of ‘1’ you can convert a number from 0 to 9 into the equvlent character by adding it to ‘0’.

#define head 1


char charArr1[130];


void setup()
{
  Serial.begin(19200);
  delay(1000);


  charArr1[0] = '1';


  if (charArr1[0] == ('0' + head))
    Serial.println("Works");
}


void loop() {}
char motHead[2];

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.