1

Can someone explain what the exclamation point in the if statement does (i.e. !strmcp)?

 string names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};

// Search for EMMA
for (int i = 0; i < 4; i++)
{
    if (!strcmp(names[i], "EMMA"))
    {
        printf("Found\n");
        return 0;
    }
}
printf("Not found\n");
return 1;
clearlight
  • 12,255
  • 11
  • 57
  • 75
  • 1
    Does this answer your question? [Using boolean values in C](https://stackoverflow.com/questions/1921539/using-boolean-values-in-c) – kaylum Jan 15 '20 at 23:02
  • The answers to this thread should help you: https://stackoverflow.com/questions/1598425/how-do-i-check-if-a-value-matches-a-string – Mark Snyder Jan 15 '20 at 23:04
  • 1
    if returns 0, aka false, when the 2 strings match, aka are identical. The ! does a logical not of that false, to get true, and enter the if when the strings are identical. Which is really when found should be printed – B. Go Jan 15 '20 at 23:05
  • The code is fine, but unclear. Better would be `if (0 == strcmp(...` – Lee Daniel Crocker Jan 15 '20 at 23:11
  • See [How do I check if a value matches a string](https://stackoverflow.com/a/1598429/15168) for relevant information. The notation `!strcmp(…)` is equivalent to `strcmp(…) == 0` — but less clear, IMO. – Jonathan Leffler Jan 16 '20 at 03:26

5 Answers5

2

strcmp() returns 0 if the strings are identical, so you need to negate it, if you use it in an if clause to assert a true statement.

If your clause is if(0), the code inside the condition will not be executed.

For completion, it returns negative if the first different character found is lower in the first string, for instance:

first parameter string: "abca"

second parameter string :abcd"

This will return negative. If it's the other way arround it will return positive.

Also, string is not usually used in C (I refer you to Jonathan Leffler's commment), you can use char*:

char *names[] = {"EMMA", "RODRIGO", "BRIAN", "DAVID"};
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 1
    This doesn't answer the question asked – Mickael B. Jan 15 '20 at 23:06
  • You'd find `string` used in the CS50 course (SO tag [tag:cs50]; SE site [CS50](https://cs50.stackexchange.com/)) — as a synonym (typedef) of `char *`. I'm not convinced it is a good choice, but that is certainly one place where it is used. – Jonathan Leffler Jan 16 '20 at 03:28
  • @JonathanLeffler, never heard of it, I'll be sure to add it to my answer. – anastaciu Jan 16 '20 at 09:14
2

For an if statement, if the expression evaluates to 0, then the block of code following the if statement is not executed. Any other value (positive or negative), will result in executing the code block. The function strcmp uses 0 to say that strings are equal because less than 0 is used to differ from greater than 0.

So in this code, we want printf("Found\n"); to be executed when the strings are equal. Since strcmp results in 0, we need to negate the value so that it becomes 1 which will result in executing that code block.

Warpstar22
  • 567
  • 4
  • 19
1

Unary operator ! is called the logical NOT operator (cf., for example, this definition at cppreference.com). ! expression returns 1 if expression evaluates to 0, and it returns 0 if expression evaluates to anything else but 0.

So the condition in if (!0) gives 1; this means, the condition is met and the if-block is entered. It has the same meaning as if(0==0)

Consequently, the meaning of

if(!strcmp(names[i], "EMMA"))

in your code is exactly the same as

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

And you already know when strcmp returns 0...

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1

The exclamation point is the C's boolean negation character. It means give me the boolean opposite of the value. A boolean is either true or false, which are 1 or 0 in the C Language.

In C, if statements execute their conditional statements if the argument is true.

if (a)   means if a is true (i.e. non-zero)
if (!a) means if a is false (i.e. 0)

Therefore:

if (a)   is the same as  if (a != 0)
if (!a) is the same as  if (a == 0)

Sometimes you'll see code that uses two exclamation points in a row "!!"

For example:

int a = !!b;

That ensures a will be ONLY 0 or 1, regardless of what the value of b is.

If b is ANY non-zero value, the ! operator will treat it as though it is true true, which it treats as being the same as 1

So:

!0 == 1
!1 == 0
!52 == 0
!25692 == 0

The second ! does the boolean inversion again, so:

!!0 == 0
!!1 == 1
!!52 == 1
!!25692 == 1
clearlight
  • 12,255
  • 11
  • 57
  • 75
-1

In C any non zero value is considered ad the logical truth, zero i considered as logical false. ! is a logical negation. So !0 (not false) will be the truth and if(!strcmp(str1,str2)) {statements} statements will be executed when str1 will be same as str2

0___________
  • 60,014
  • 4
  • 34
  • 74