L. Kratky L. Kratky - 7 days ago 5
C Question

Check if input matches word in C

I am building a program, which needs to read input from user in form of command such as 'command 12' where command is specific word and needs to be stored in separate variable, number after it needs to be in its variable too.

I created variable in which is word that needs to match the command and int variable for storing.

char rule[5] = {'r', 'u', 'l', 'e', '\0'};
char command[6];
int cmd_num;


And then used scanf to read it and then compare it by strcmp.

scanf("%s %d", &command, &cmd_num);
if ( (strcmp(command, rule)) != 0)
{
printf("Error.\n");
return 1;
}


Yet it doesn't work as I would expect it. I googled for hours, but I am not capable of finding how to properly store this string and number after it. Could anyone help me to understand how string is stored? It would help me to understand why strcmp doesn't work the way I'd expect it.

Or is there any better way to check if user input is matching certain word? The number after it seems to be problem for me. I tried to play around with gets functions but I was not able to come to results. Thank you in advance.

Answer

First, you can directly define rule as string using double quotes (and to avoid to write into it, make it a constant pointer):

const char *rule = "rule";
char command[6];
int cmd_num;

When you use scanf, limit length to read to avoid buffer overruns (using %5s which is the maximum size of the array minus zero-terminator), and don't pass address for arrays/pointers, because scanf can already access the address of the string to write into:

nb_items = scanf("%5s %d", command, &cmd_num);

Note that you can know how many items have been scanned (declare int nb_items;). For instance, if the user forgets to input a number or inputs a non-number, you'll get nb_items==1 and you'll know that cmd_num hasn't been initialized.

The main issue here is that you had undefined behaviour trying to store the result of your scanf in the address of the address of the array.

Comments