I want another condition –still maintaining a fast execution time but safer— where i return false if either or both strings is empty:
int speicial_strcmp(char *str1, char* str2 )
{
if(*str1==*str2 =='\0')
return 0;
return strcmp(str1,str2);
}
No, that’s not a good way to do it, because it doesn’t work.
will get evaluated as:
In other words, because the bool will get promoted to an integer, your test will return true whenever the strings start with different characters (
tmp1will be false, which gets converted to 0, and sotmp2becomes true)Don’t try to outsmart the compiler. Writing fast code is not about writing as few lines of code as possible, or even as short lines as possible. Even if chaining together
==in this manner was meaningful, there’s no reason why it’d be faster. Just write code that you understand, and can write correctly.