Programming Fundamentals · Interview question

What counts as "true" in a C if condition?

A strong answer

Any non-zero numeric value. Zero is the only "false" value; 1, -1, 42, 3.14f, 'A' (which is 65), and any non-null pointer are all "true." This is a holdover from C's pre-bool era, the language was built on the convention that integers double as truth values. The <stdbool.h> header gives you true and false as 1 and 0, but the underlying behavior is unchanged. The interview-favorite consequence is if (x = 5), which assigns 5 to x and then treats the result as a non-zero "true," so the branch always runs.

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

Conditionals

How a program makes decisions. if, else if, and else, with the dangling-else trap and the ternary shortcut for one-line choices.

More Conditionals questions

Browse all 472 interview questions