What is the difference between authenticity and string?

Asked 1 years ago, Updated 1 years ago, 40 views

I don't know how to compare the authenticity of the C language with the string, so please let me know.

1:True or false
"Regarding authenticity, the introduction says ""false is 0; true is not 0."", but I feel that if (strcmp(str1, str2)==0), true and false are reversed. Does this 0 mean something different from true or false?"

2:About string comparison
When comparing strings, the strcmp function is used, but the variables in the strings are certainly incorrect in comparison, but when comparing pointer variables and strings themselves, they seem to be correct.
If(str3=="ABC") seems to be working well, but is this the wrong way to compare just because it looks like that?

int main(void){
    char str1[10] = "ABC";
    char str2[10] = "ABC";
    char*str3 = "ABC";

    if(strcmp(str1,str2)==0){
        puts("true");
    } else {
        puts("false");
    }

    if(str3=="ABC"){
        puts("str3 = true");
    } else {
        puts("str3=false";
    }
    return 0;
}

c

2022-09-30 21:31

2 Answers

Does this 0 mean something different from the true or false 0?

"It depends on how you receive the word ""meaning"", but basically you can think of it as a different way."

First of all, the "true or false" value in C language differs from the Boolean type value in other languages such as Java, and the substance is just a number.The situation varies slightly depending on the version of the C language itself, but all of them simply assign 0 a false meaning.

Therefore, you can assign another meaning to 0. The strcmp function has three types of comparisons: small, equal, and large, which are represented by integer return values, so 0 has an equal meaning.

Based on these historical backgrounds, it is often interpreted that the program strcmp(str1, str2)==0 "checks whether the return value of strcmp means 'equal' or 'false'.In this sense, this 0 is a different meaning from the authenticity value 0.

if(str3=="ABC") seems to be working well, but is this the wrong way to compare just because it looks like that?

Yes, == should not be used directly when comparing strings of type char* in C language. str3=="ABC" is actually comparing to a pointer, not a string.In addition, Comparison with pointers may cause unexpected behavior.

As an example to illustrate that strcmp should be used, I wrote a program that confirmed that == and strcmp behave differently.

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

int main() {
    char*s = "ABCDEF";
    char*t=s+3;

    if(t=="DEF"){
        printf("==:equal\n";
    } else {
        printf("==:not equal\n";
    }

    if(strcmp(t, "DEF")==0){
        printf("strcmp:equal\n";
    } else {
        printf("strcmp:not equal\n";
    }

    return 0;
}

The pointer t points to the null-terminated string DEF, but Try running on the Wandbox as follows:

==—Not equal
strcmp —Equivalent


2022-09-30 21:31

Does this 0 mean something different from the true or false 0?

No, I'm using this 0 to compare the strcmp return value.

if(str3=="ABC") seems to be working well, but is this the wrong way to compare just because it looks like that?

String literal compatible with variable 'str3'. Did you intend to use strcmp()instead? or comparison with string literal results in unspecified behavior, so I prefer to use strcmp.


2022-09-30 21:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.