Why == doesn't work on strings in C

Professional Biography:
In the technology realm, others see code. I see art. My journey as a software engineer is fueled by a steadfast conviction in the power of creativity to transform our world and the ability of technology to unleash human potential.
As a self-taught developer from Nigeria, I have taken a unique route fueled by a constant inquiry and an insatiable thirst for invention. I spent my five years as a developer growing from a lone learner to a versatile full-stack developer and enthusiast for artificial intelligence. Today, my technological landscape spans web development, artificial intelligence, and blockchain technologies, each established through learning about multiple frameworks.
Technical Skills:
In terms of technical skills, I possess a broad breadth and depth. I know multiple programming languages - Python, JavaScript, Ruby, and C - and possess exceptional knowledge of multiple web technologies - React, Next.js, Node.js, Django, and upcoming frameworks. When I approach code development, I don't just think of it as code. I think of it as compositions. Each line of code is like a musical note bringing functionality together to create a symphony.
Professional Philosophy:
More than skills, I have a vision to create opportunities. Having seen dauntless ambition and unmet creativity in uninviting environments, I believe technology has the possibility of leveling the playing field. I find coding far more relevant than code; to me, it's about giving people a chance to unearth their expression and potential.
Current Frontiers:
I'm currently digging deep into the intersections of AI, web development and blockchain technologies. My excitement surrounding cryptocurrencies and programmable assets stems from a mindset I have toward a more fluid, easier to access digital ecosystem. I view these technologies as tools for innovation rather than things that are far off in the future.
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!






