3

There is a question that makes me confused during learning with the Harvard University CS50 course. The following is the question that bothers me for a long time.

For the following code, it wants to compare the string called "EMMA" with the array called "names" which contains 4 names inside.

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
    // An array of names
    string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};

    // Search for EMMA
    for (int i = 0; i < 4; i++)
    {
        if (strcmp(names[i], "EMMA") == 0)
        {
            printf("Found\n");
            return 0;
        }
    }
    printf("Not found\n");
    return 1;
}

In the above codes, it uses if (strcmp(names[i], "EMMA") == 0) to check the name "EMMA".

However, it would also run if I write the code in another way like if I replace if (strcmp(names[i], "EMMA") == 0) with if (!strcmp(names[i], "EMMA")), and it turns out the same answer "Found".

If my memory serves me right that the exclamation ! in C means "NOT". In the first method, it uses two equal signs to express the value turns out the same with 0. But in the second method, it uses exclamation notation in front of the function strcmp. I am not familiar with the meaning of why it also gives the same output in the second method even though I have looked up the definition of function strcmp.

Moreover, it would be great if someone could tell me what value would the strcmp function gives and what is the proper expression in the plain words?

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Ricky.L
  • 47
  • 4
  • 1
    The unary `!` operator performs a logical "not" operation on its operand. `!x` is equivalent to `x == 0`. – Tom Karzes Apr 07 '20 at 14:44
  • 2
    This is basic boolean algebra and not related to strings. In C, everything that is not zero is to be regarded as "true". Meaning that `!whatever` equals 0 if "whatever" is non-zero. And that's pretty much it. – Lundin Apr 07 '20 at 14:49
  • Thanks Tom and Lundin, it useful! – Ricky.L Apr 07 '20 at 15:02

4 Answers4

5

The ! operator is used for boolean negation.

!0 is the same as 1 (true)

!1 is the same as 0 (false)

In fact, every non-zero integer is true in C and only 0 is false.

So, if strcmp(names[i], "EMMA") == 0 is true

Then !strcmp(names[i], "EMMA") is also true because !0 is true.

Moreover, it would be great if someone could tell me what value would the strcmp function gives and what is the proper expression in the plain words?

Check this link.

In short,

strcmp can return three possible values:

0, if both the strings are equal

a positive integer, if the first string is greater than the second string

a negative integer, if the first string is smaller than the second string

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
3

As you can see here, the strcmp function returns 0 if the two strings are identical, otherwise it returns either a positive or negative integer. The reason the second method also works is due to a property of C (and a lot of other languages) where even though the value has the type integer, it can be coerced into a boolean (true or false) type. What this means is that, when you do !0 you will get the value true, because 0 evaluates to false and applying the ! operator flips it to a true. Any integer value (whether positive or negative) other than 0, will result in the value true, so for example, !1 will result in false because the ! flips the value.

awarrier99
  • 3,628
  • 1
  • 12
  • 19
1

The answer is in your question, strcmp will return a non-zero value if the strings are different and zero if they are equal.

If you apply ! logical negation operator to 0, it will evaluate to true, if you apply it to non-zero value it will evaluate to false, so the conditon has the same logical value in both methods.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
1

look at this

int a=1;
while(a)
{
//do something
}

the above example means run while a!=0 and if you use this

int a=1;
while(!a)
{
//do something
}

this means run while a==0

this if(!strcmp(names[i], "EMMA")) is a Boolean. which says if !strcmp(names[i], "EMMA") is true enter the statement which means if !0 happens enter the statement , because !false is equivalent to true.

hanie
  • 1,863
  • 3
  • 9
  • 19