Recall & Review
beginner
What function in C is commonly used to convert a string to an integer?
The
atoi() function converts a string to an integer. It reads the string until it finds a non-numeric character.Click to reveal answer
intermediate
What is the difference between
atoi() and strtol()?atoi() converts a string to an int but does not handle errors well. strtol() converts to long int and provides error checking by using an end pointer and errno.Click to reveal answer
intermediate
How can you check if a numeric argument is valid when parsing a string in C?
Use
strtol() with an end pointer. If the end pointer points to the string's end, the whole string was a valid number. Also check errno for overflow or underflow.Click to reveal answer
beginner
What header file must be included to use
strtol()?Include
<stdlib.h> to use strtol().Click to reveal answer
intermediate
Why is it important to check the base parameter in
strtol()?The base parameter tells
strtol() which number system to use (e.g., 10 for decimal, 16 for hex). Using the wrong base can cause incorrect parsing.Click to reveal answer
Which function converts a string to a long integer and allows error checking?
✗ Incorrect
strtol() converts strings to long integers and provides error checking via an end pointer and errno.What does
atoi() return if the string does not start with a number?✗ Incorrect
atoi() returns 0 if the string does not start with a valid number.Which header file is required to use
strtol()?✗ Incorrect
strtol() is declared in <stdlib.h>.What does the 'endptr' argument in
strtol() help you determine?✗ Incorrect
The 'endptr' points to the character after the last parsed digit, helping detect invalid characters.
If you want to parse a hexadecimal number string, what base should you pass to
strtol()?✗ Incorrect
Use base 16 to parse hexadecimal numbers.
Explain how to safely parse a numeric argument from a string in C.
Think about how to detect invalid input and overflow.
You got /4 concepts.
Describe the limitations of using atoi() for parsing numbers.
Consider what happens if the input is not a number.
You got /4 concepts.