LearnFrenzy
Sign in Take a free test

Error Handling · Error Handling MCQ · Question 11

Why is strtol preferred over atoi? Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

Why is strtol() preferred over atoi() for user input?

  • A atoi is slower
  • B atoi only handles positive numbers
  • C strtol works only on hex
  • D atoi cannot report invalid input or overflow; strtol gives the end position and sets errno to ERANGE Correct
Correct Answer: Option D

atoi("abc") returns 0 with no way to tell it from "0", and an out-of-range value is undefined behaviour.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links