C - Comparing string literal with character array

I have written a complete version of what I think you are trying to do:

    #include <string.h>
    void main()
    {

       char command[20];
       scanf("%s",command);

       // command and "hello" can be less than, equal or greater than!
       // thus, strcmp return 3 possible values
       if (strcmp(command, "hello") == 0)
       {
          printf("\nThe user said hello!");
       }

    }

Several people have commented about using scanf and they are correct, except that a new programmer has to start somewhere in learning this stuff, so don't feel too bad we are all learning...

Hope this helps.


strcmp returns 0 when the strings are the same. I have code that uses strcmp comparing character arrays to string literals, and I was quite confused when it wasn't working. Turns out it was wrong for me to assume it would return 1 when the string are the same!

Maybe you've made the same mistake?


I think this is a perfect starting point for you:

http://www.wikihow.com/Compare-Two-Strings-in-C-Programming

It's probably written at the right level for you. Good luck and welcome to stackoverflow!