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!
strcmp()
is the way to go. – Postpaidscanf()
. Forget that function altogether. You are looking forfgets(command, sizeof command, stdin)
instead. – Postpaid%s
by default, it pretends to understand regexes but it doesn't, etc, etc... – Postpaidscanf("%20s", command);
. Butfgets()
followed bysscanf()
is more robust. – Palatalizedscanf()
format strings can very easily be misleading to someone who just started out with programming. – Postpaid