C - Comparing string literal with character array

arrays, c, character, strcmp, string

Solution

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?

Problem

I am new to C and am still a bit confused about how to use strings via character arrays. In my C program, I am accepting commands from the user: ``` char command[20]; scanf("%s",command); ``` Of course, afterwards I want to figure out what command they typed (something similar to: "`if (command == "hello")`, then do something"). I know this is not possible in C because I am comparing a string literal to a character array, but what would be a good way to it? I have tried using `strcmp(command, "hello")` and still got errors. Any advice you can provide would be very appreciated. Thank you!

Original source