8

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!

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Irina
  • 1,333
  • 3
  • 17
  • 37
  • 3
    What "errors", precisely? `strcmp()` is the way to go. –  Aug 02 '13 at 21:33
  • Also, the single worst error I see in your code is the use of `scanf()`. Forget that function altogether. You are looking for `fgets(command, sizeof command, stdin)` instead. –  Aug 02 '13 at 21:34
  • (The main reason behind not using scanf; there's no limit to how much it could scan. What if the user inputs 21 characters?) – Dennis Meng Aug 02 '13 at 21:35
  • Furthermore, a string literal **is** a character array itself, by the way. –  Aug 02 '13 at 21:35
  • @DennisMeng And it doesn't scan whitespace using `%s` by default, it pretends to understand regexes but it doesn't, etc, etc... –  Aug 02 '13 at 21:36
  • That too. But letting the user overflow your buffer is scarier than screwing up regexes. – Dennis Meng Aug 02 '13 at 21:37
  • @H2CO3: It doesn't pretend to understand regexes, it just implements a scanning syntax that's less powerful than regexes. Buffer overflow can be avoided by using, for example, `scanf("%20s", command);`. But `fgets()` followed by `sscanf()` is more robust. – Keith Thompson Aug 02 '13 at 21:42
  • @KeithThompson What I was referring to is merely that the syntax of `scanf()` format strings can very easily be misleading to someone who just started out with programming. –  Aug 02 '13 at 21:45

4 Answers4

15

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.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21
  • Yes, you were all right. The problem was that I thought strcmp was simply a boolean function, when in reality it returns 3 possible values. And I will try to use fgets once I become more familiar with it. The reason why I am using scanf is that I need to read the command typed by the user up until the first white space (not the whole line, since I need to read two commands per line). Once I figure out how to control what I am reading with fgets, I will definitely use it. Thanks for the advice with fgets, and thanks so much everybody for the thorough answers! You are so helpful! – Irina Aug 03 '13 at 07:41
  • You mind upvoting useful answers and selecting one as the right answer? – gravitas Aug 06 '13 at 19:07
  • RSinghS - I have tried but I need to have 15 reputation to upvote answers. I will definitely upvote the useful answers once I am able to! :) – Irina Aug 09 '13 at 05:40
  • @Irina, I think if it is your question you can select the "correct" answer, from your point of view!! This is different than upvoting but it does provide useful feedback. – JackCColeman Aug 09 '13 at 06:00
  • Understood and done. Thanks and sorry! Still new around here. :) – Irina Aug 10 '13 at 06:12
7

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?

gravitas
  • 703
  • 4
  • 16
  • 2
    Indeed, making assumptions is the incorrect way of writing programs - one should read the documentation instead. –  Aug 02 '13 at 21:37
2

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!

spartygw
  • 3,289
  • 2
  • 27
  • 51
0

When talking about string in C, it normally takes two forms: 1. a character array, 2. a character pointer. Most of the time, they are interchangeable. For example:

char *cmd_ptr = "command1";
char cmd_array[20] = "command2";
printf ("cmd1: %s cmd2: %s\n", cmd_ptr, cmd_array);

The main difference for the above definition is that for cmd_ptr you could not change its content like cmd_ptr[0] = 'a'; for cmd_array you could change any element in the array.

But you could do cmd_ptr = cmd_array; then you could make changes through cmd_ptr as it points to the same location as cmd_array.

qunying
  • 428
  • 3
  • 4