I was reading the proper way of comparing two strings is to use the strcmp() function.
However I am not sure why the first comparison below would leads to the condition True (i.e. the contents inside the first "if condition" was able to be printed).
#include <stdio.h>
#include <string.h>
int main()
{
printf("Hello World\n");
char *x;
char *w = "oliver bier";
char *w2 = "oliver bier";
x = w;
if(x == "oliver bier") { // what is being compared here?, why this leads to True
printf("This is comparing string also????\n");
printf("Or this is comparing the type of the variable x and type of oliver bier i.e. a string??");
}
if ( strcmp( x, w2) == 0 ) { // I was reading this is the correct way of comparing strings
printf("I think this is the correct way to compare two strings\n");
}
if(x == w) { // what is being compared here?
printf("This is expected!! since x, w stored same address");
}
return 0;
}
so basically why is it that if(x == "oliver bier") would evaluate to true as well? I thought x is a pointer to character, and "oliver bier" is a string.