简体   繁体   English

C 编程中的感叹号和`strcmp` Function

[英]The Exclamation Notation and `strcmp` Function in C programming

There is a question that makes me confused during learning with the Harvard University CS50 course.在学习哈佛大学CS50课程的过程中,有一个问题让我很困惑。 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.对于以下代码,它希望将名为“EMMA”的字符串与名为“names”的数组进行比较,其中包含 4 个名称。

#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".在上面的代码中,它使用if (strcmp(names[i], "EMMA") == 0)来检查名称“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 (strcmp(names[i], "EMMA") == 0)替换为if (,strcmp(names[i], "EMMA")) ,结果是相同的答案“找到”。

If my memory serves me right that the exclamation !如果我的 memory 对我有帮助,那感叹号! in C means "NOT".在 C 中表示“不”。 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 .在第一种方法中,它使用两个等号来表示与 0 相同的值。但是在第二种方法中,它在 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 .我不熟悉为什么它在第二种方法中也给出相同的 output 的含义,即使我已经查找了 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?此外,如果有人能告诉我strcmp function 给出什么值以及用简单的话表达的正确表达方式,那就太好了?

The ! ! operator is used for boolean negation.运算符用于 boolean 否定。

!0 is the same as 1 (true) !01相同(真)

!1 is the same as 0 (false) !10相同(假)

In fact, every non-zero integer is true in C and only 0 is false .事实上,每个非零 integer 在 C 中都是true ,只有 0 是false

So, if strcmp(names[i], "EMMA") == 0 is true因此,如果strcmp(names[i], "EMMA") == 0为真

Then ,strcmp(names[i], "EMMA") is also true because !0 is true .然后,strcmp(names[i], "EMMA")也为真,因为!0true

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?此外,如果有人能告诉我 strcmp function 给出什么值以及用普通话表达的正确表达方式,那就太好了?

Check this link .检查此链接

In short,简而言之,

strcmp can return three possible values: strcmp可以返回三个可能的值:

0 , if both the strings are equal 0 ,如果两个字符串相等

a positive integer , if the first string is greater than the second string如果第一个字符串大于第二个字符串,则为正 integer

a negative integer , if the first string is smaller than the second string如果第一个字符串小于第二个字符串,则为负 integer

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.如您在此处看到的,如果两个字符串相同,则strcmp function 返回 0,否则返回正或负 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.第二种方法也有效的原因是由于 C (和许多其他语言)的属性,即使该值的类型为 integer,它也可以强制转换为 Z84E2C64F38F78BA3EA5C905ABA5 类型。 What this means is that, when you do !0 you will get the value true , because 0 evaluates to false and applying the !这意味着,当您执行!0时,您将获得值true ,因为 0 评估为false并应用! operator flips it to a true .运算符将其翻转为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 !除 0 以外的任何 integer 值(无论是正值还是负值)都将导致值true ,例如, !1将导致false ,因为! flips the value.翻转值。

The answer is in your question, strcmp will return a non-zero value if the strings are different and zero if they are equal.答案就在你的问题中,如果字符串不同, strcmp将返回一个非零值,如果它们相等,则返回零。

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.逻辑否定运算符为0 ,它将评估为true ,如果将其应用于非零值,它将评估为false ,因此条件在两种方法中具有相同的逻辑值。

look at this看这个

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

the above example means run while a!=0 and if you use this上面的例子意味着在a!=0时运行,如果你使用它

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

this means run while a==0这意味着在a==0时运行

this if(,strcmp(names[i], "EMMA")) is a Boolean.这个if(,strcmp(names[i], "EMMA"))是 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 .这表示 if ,strcmp(names[i], "EMMA")为 true 进入语句,这意味着如果!0发生进入语句,因为!false等同于true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM