Why is it "a"! = "a"?

Asked 2 years ago, Updated 2 years ago, 100 views

void main() {
    if("a" == "a")
      printf("Yes, equal");  
    else
      printf("No, not equal");
}

Output: No, not equal

I thought it would be true because the two of them are the same string, but why did the false appear?

string c

2022-09-22 22:18

1 Answers

C, The string surrounding C++ to " always creates a new char array.

What you just compared is the addresses of the two char arrays created separately, so of course they are different.

You must use the strcmp function to compare strings.

(Note, in most script languages, "a" == "a" is true.)


2022-09-22 22:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.