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.
Answer checked How we write and check questions
Discussion