Practice mode C Language

What is printed by this C code: printf(“%d”, atoi(“42abc”));?

Correct answer D. 42

Explanation

atoi skips leading white space, reads as many digits as form a valid integer, and stops at the first character that cannot belong to the number, ignoring the rest. So "42abc" gives 42. atoi cannot report errors, returning 0 when nothing converts and having undefined behaviour on overflow, which is why strtol with its end pointer is preferred.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.