2

I am trying to use fgets to call different functions depending on what the string the user inputs. I know I will need to use strtok later since there will be spaces for, for example, "load 12". But now, I am confused about using strcmp to compare strings being input. I know strcmp can be used like this:

int check;
char string[10] = "test";

check = strcmp(string, "test");
// Check will be 0 if true

if (check == 0)
{
    printf("same string\n");
}
else
{
    printf("not the same\n");
}

Can it be a boolean value like true and false? If "test" is actually "test", boolean value becomes true, then I will use the boolean value in the if statements.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • You're not using it as a boolean. `check` is an integer, and comparing it with another integer. – Barmar Apr 07 '16 at 07:59
  • Yes you can, but the `strcmp` return value in boolean context would be true (non-zero value) if the strings **differ** and false if they don't... that's why I prefer comparing ` == 0`. – Antti Haapala -- Слава Україні Apr 07 '16 at 08:07
  • 1
    If you want to test against the boolean values `true` and `false` not only will the logic be inverted, but the function does not necessarily return `1`, only a positive value, so you can't use `if(strcmp(string, "test") == true)` – Weather Vane Apr 07 '16 at 08:07
  • There is no standard boolean type in C. `int`s are used as truth values : `0` stands for false and every number that is non null stands for true. – Boiethios Apr 07 '16 at 08:09
  • @Morovaille please [see this question](http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type) – Weather Vane Apr 07 '16 at 08:11
  • @WeatherVane I did not know bool, true and false are standard macros in C99, but it is not a real bool type, there are just macro to `int`s. – Boiethios Apr 07 '16 at 08:14
  • @Morovaille one answer in that question says: C99 added a builtin `_Bool` data type ([see Wikipedia](http://en.wikipedia.org/wiki/Boolean%5Fdata%5Ftype#C99) for details), and if you `#include `, it provides `bool` as a macro to `_Bool`. – Weather Vane Apr 07 '16 at 08:17
  • "Boolean values still behave as integers, can be stored in integer variables, and used anywhere integers would be valid, including in indexing, arithmetic, parsing, and formatting." – Boiethios Apr 07 '16 at 08:19
  • @Morovaille you went off my point, which was that `strcmp` does not necessarily return `1`, or `true`, and quite likely does not. – Weather Vane Apr 07 '16 at 08:36

5 Answers5

2

You can write and use a wrapper/helper function to reach this goal, something like this:

bool are_equal(char* a, char* b)
{
    return strcmp(a, b)==0;
}
RhinoDevel
  • 712
  • 1
  • 12
  • 25
1

if (strcmp(string, "test") == 0) will check if both strings are the same.

The return value of strcmp(..) is an integer, and if both strings are the same, the return value is an integer 0. So effectively you are comparing if 0 == 0, and so that is ok.

Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
1

strcmp() returns an integer value 0 when the strings are equal. For boolean checks, everything besides integer 0 in c counts as true, and integer 0 coounts as false.

So yes, your approach will work.

Magisch
  • 7,312
  • 9
  • 36
  • 52
0

The problem with strcmp and other such functions, is that they go back to the time when C didn't have a boolean type. So they define "not equal" as an integer number either larger or smaller than zero, depending on if the first string is larger or smaller than the second. Inside the implementation of strcmp, you might find something like

int strcmp(const char *s1, const char *s2)
{
  ...
  if(*s1 != *s2)
  {
    return *s1 - s2*
  }
  ...
  return 0;
}

Meaning that in case the strings are not equal, the function might return any number, equal to the difference between the symbol codes for the first non-equal characters found.

You can solve this by designing wrappers around strcmp:

inline bool is_equal (const char* s1, const char* s2)
{
  return strcmp(s1, s2) == 0;
}

inline bool is_greater (const char* s1, const char* s2)
{
  return strcmp(s1, s2) > 0;
}

inline bool is_less (const char* s1, const char* s2)
{
  return strcmp(s1, s2) < 0;
}

And now you might notice that the original definition of strcmp's return value wasn't as dumb as it seemed, because it meant they could write 1 function instead of 3. Which is very handy when you write comparison functions for bsearch, qsort etc.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

You can actually use strcmp as a boolean, because what you want to check is if the strings are the same; so that will return a 0 (which also means false). So you just write:

if (!strcmp(string, "test")) //!0 = true
{
    printf("same string\n");
}
else
{
    printf("not the same\n");
}