0
0
Cprogramming~5 mins

Parsing numeric arguments - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Astrtol()
Batoi()
Csprintf()
Dstrlen()
What does atoi() return if the string does not start with a number?
AThe first character
B-1
CAn error code
D0
Which header file is required to use strtol()?
A<stdio.h>
B<stdlib.h>
C<string.h>
D<math.h>
What does the 'endptr' argument in strtol() help you determine?
AWhere the number parsing stopped
BThe base of the number
CThe length of the string
DThe maximum value parsed
If you want to parse a hexadecimal number string, what base should you pass to strtol()?
A8
B10
C16
D2
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.