Skip to main content

Command Palette

Search for a command to run...

Checking equality of strings

Updated
•1 min read•View as Markdown

Program:

int main(){
    printf("1==1 : %d\n", 1==1);
    printf("'a'=='a' : %d\n", 'a'=='a');
    printf("hello==hello : %d\n", "hello"=="hello");
    char s1[] = "hello";
    char s2[] = "hello";
    printf("hello==hello strings as char arrays : %d\n", s1 == s2);
    char *s3 = "hello";
    char *s4 = "hello";
    printf("hello==hello strings as char pointers : %d\n", s3 == s4);
    printf("hello==hello one string as char pointer, other as char array : %d\n", s1 == s4);
    printf("hello==hello one string as char pointer, other as a literal : %d\n", s3 == "hello");
    printf("hello==hello one string as char array, other as a literal : %d\n", s1 == "hello");
}

Output:

Note:

Recall that 0 means false and any non-zero value means true, as the result of comparison operators.

15 views

More from this blog

P

Programming Tutorials

89 posts