I have got two strings (string literals in that case), say
char *name = "Fotis";
char *second_name = "Fotis";
and I have two ways to compare them, which are both giving me an accurate result. The first one is with the equality operator (==
), likewise:
if (name == second_name)
printf ("Great success!\n");
and the second one is through the strcmp
function available in string.h
:
if (strcmp (name, second_name) == 0)
printf ("Great success!\n");
My question is: Which one of the two is the most {efficient | idiomatic} way to compare two strings in C? Is there another, more idiomatic way?