Programming Fundamentals · Interview question

What's wrong with if (strcmp(a, b))?

A strong answer

strcmp returns 0 when the strings are equal, negative if a comes before b, positive otherwise, it's modeled after subtraction, not equality. So if (strcmp(a, b)) is true exactly when the strings are different, which is almost always the opposite of what the author meant. The correct form is if (strcmp(a, b) == 0) for equality, or if (strcmp(a, b) != 0) for inequality. This is one of the most common C beginner bugs because it looks natural, "if compare returns truthy, they're equal", but the semantics are inverted.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Arrays & Strings

A contiguous block of same-type values. How arrays work in memory, why indexing starts at 0, and the null-terminated convention that defines C-strings.

More Arrays & Strings questions

Browse all 472 interview questions