Why is “a” != “a” in C?

What you are comparing are the two memory addresses for the different strings, which are stored in different locations. Doing so essentially looks like this:

if(0x00403064 == 0x002D316A) // Two memory locations
{
    printf("Yes, equal");
}

Use the following code to compare two string values:

#include <string.h>

...

if(strcmp("a", "a") == 0)
{
    // Equal
}

Additionally, "a" == "a" may indeed return true, depending on your compiler, which may combine equal strings at compile time into one to save space.

When you’re comparing two character values (which are not pointers), it is a numeric comparison. For example:

'a' == 'a' // always true

Leave a Comment