Recall & Review
beginner
What does the
atoi function do in C?It converts a string representing a number into its integer value. For example,
"123" becomes the integer 123.Click to reveal answer
beginner
What should
atoi do when the string contains leading spaces?It should ignore all leading spaces before starting to convert digits into an integer.
Click to reveal answer
beginner
How does
atoi handle signs like '+' or '-' in the string?If the string starts with '+' or '-',
atoi uses it to determine if the resulting integer is positive or negative.Click to reveal answer
intermediate
What happens if
atoi encounters non-digit characters after reading digits?It stops converting at the first non-digit character and returns the integer value parsed so far.
Click to reveal answer
advanced
How should
atoi handle integer overflow or underflow?It should clamp the result to the maximum or minimum integer value allowed (e.g.,
INT_MAX or INT_MIN) to avoid overflow.Click to reveal answer
What is the output of
atoi(" -42abc")?✗ Incorrect
Leading spaces are ignored, '-' sign makes the number negative, and parsing stops at first non-digit character 'a'.
If the input string is
"+1234", what integer does atoi return?✗ Incorrect
The '+' sign indicates a positive number, so the integer returned is 1234.
What should
atoi return for the input "words and 987"?✗ Incorrect
Since the string does not start with digits or sign,
atoi returns 0.How does
atoi treat the string "2147483648" if INT_MAX is 2147483647?✗ Incorrect
The number exceeds
INT_MAX, so atoi clamps it to INT_MAX.Which step is NOT part of a typical
atoi implementation?✗ Incorrect
Letters are not converted to ASCII values; parsing stops when a non-digit character is found.
Explain step-by-step how to convert a string like " -123abc" to an integer using the atoi logic.
Think about reading the string from left to right carefully.
You got /6 concepts.
Describe how to handle integer overflow when converting a string to an integer with atoi.
Remember the limits of 32-bit signed integers.
You got /3 concepts.
