Implement parse_quantity using strtol. Accept only a complete base-10 integer in range 0..100000, reject whitespace-only or trailing characters, and report success through the return value.
Requirements
Set errno before strtol
Check that at least one digit was consumed
Reject trailing non-space characters and range overflow
Example
Input
42
Expected output
success, 42
Show a hint
Write down the input contract and the failure cases before coding.
local runtime
RUNTIME OUTPUT
Use your C runtime locally, then compare with solution.c.
Use your local runtime, then review the reference solution
LEARN FROM THE SOLUTION
Why the solution works.
strtol provides three signals that a cast cannot: where parsing stopped, whether the value overflowed, and the signed value itself. The end-pointer loop allows harmless trailing whitespace while rejecting input that only looks numeric at the beginning.
What this exercise teaches
argv validation
errno and range checks
Conversion end pointers
A PRACTICAL PLAN
Work through Validate a command-line integer with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.c.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.c only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Validate a command-line integer” teach?
This intermediate C exercise focuses on argv validation, errno and range checks, Conversion end pointers. Its requirements define the exact behavior to implement before you write code.
How should I validate this C solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Write and compile the starter in your local C environment, exercise the example and edge cases, then compare your approach with solution.c.
When should I open the reference solution?
Attempt Validate a command-line integer first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.