Why == doesn't work on strings in C

Why == doesn't work on strings in C

ยท

3 min read

Have you ever wondered why we use the equality sign == to compare integers, floats and characters but not strings? Hmmmm..., sounds like something fishy is going on with the compiler, this can't be on purpose ๐Ÿค”

Honestly, what are strings?

The concept of string is greatly misunderstood among developers new to c; this is very normal - even the computer doesn't understand strings.

In lay man terms, a string is a textual data. This means that it is a form of data made up of characters. In C, we create strings primarily by putting a bunch of characters within double qoutes.

"This is a string"

A string can be declared using two different types

  • a character array: char[]

  • a character pointer: char *

The choice of which to use is most times based on preference or convenience.

In C however, a string is a bunch of characters placed right beside themselves in memory, the string ends where there is a null-byte character \0.

A string can only be stored as a character address or as an array. Either way, a string is an address to a character.

Why == can't be used with strings

If a string is an address, then comparing a string with another string is basically comparing their addresses; it would only be true if it's the same string.

/* Example1 */

int main(void)
{
    char *s1 = "alex";
    char s2[] = "alex";

    s1 == s1; /* true */
    s1 == s2; /* false */
}

How to compare strings

To compare strings, the standard library provides a strcmp function which you can get via string.h.

NOTE: the function should return 0 if the two strings match and a non-zero number if they don't.

Example 1
#include <string.h>

int main(void)
{
    char *s1 = "alex";
    char s2[] = "alex";

    strcmp(s1, s1); /* 0 */
    strcmp(s1, s2); /* 0 */
}

Example 2
int main(void)
{
    char *s1 = "alex";

    s1 == "alex"; /* true */
    "alex" == "alex"; /* true */
}

๐Ÿ˜• what's happening?

This is called string interning. String interning is done to save space and computational time and therefore increases the speed and efficiency of your program.

In the above example, the strings are not just equal, they are the same. They both start at the exact same address, so when we use == to compare them, it compares their starting address and returns true since they all have the same starting address.


Is there anything that I should have covered which I didn't? Let me know in the comments; I'm open for discussion.

Till we meet again, keep doing hard things!

ย