Setting and Comparing char vars

How do you set and compare char vars?

     char digittogether[4];
     digittogether = "123";
      if (digittogether == "123"){
        Serial.println("123 Code entered");

I get a compile error on tryine to set the digittogether = "123". I hard coded the digittogether because the if didn't seem to be working, so I assume I'm doing something wrong with the setting and the compare. What is this newbie missing with my vars?

Those are strings, not a char.

try strcmp():

Sorry, but you can't compare two arrays with the '==' operator. One way to do that is to use the strcmp() function:

   if (!strcmp(digittogether,"123")
   {  /* what to do if they're equal */
   {  /* do something else if they're not */

Thank you but I still want to know how to set a char var in code. I've tried digittogether = '123'; How do you set a char var? I'm looking up the function for the compare - thank you

The scanf () did allow me to do the comparison - THANK YOU!

Through I'd still like to know How do you set a char in code. Or is sprintf () the best (only) way. I've seen in the references how to declare the char with a value, but how to set it in code?

Thank you

For a single char, you can code:

   char c = 'a';

and for an array of chars you can:

   char d[] = "abcdefghij...";

and you can use strcpy() to do:

   char name[10];


I'm missing something - The "123 Code Entered" is printed out no matter what.


      if (!scanf(digittogether,"123")){
        Serial.println("123 Code entered");

Where did you get scanf() from? The suggestion was to use strcmp(). (As in STRING COMPARE)

:* Looks like i copy/paste the wrong function. That would explain the problem - Thank you