Input and parsing.
Treat input as text to bound, convert, and validate before use.
The idea behind the syntax.
Input from a terminal or file is external data. It may end early, contain unexpected characters, or exceed a buffer. A program should distinguish reading bytes from interpreting those bytes as a number or command. fgets reads at most one less than the buffer capacity, adds a zero terminator on success, and may leave the newline in the buffer. It returns a null pointer on end-of-file or error, which should be checked before parsing.
scanf can convert formatted input directly, but its return value counts successful assignments and its string conversions need field widths to avoid overflow. For many interactive programs, reading a bounded line and converting it with strtol or a related function gives clearer error handling. A conversion routine can report where parsing stopped, letting the program reject trailing junk instead of silently accepting a prefix.
What C guarantees.
strtol uses an end pointer to tell where numeric conversion ended. If no digits were found, the end pointer equals the original string. errno can report range errors, and the converted long still needs a range check before narrowing to int. Whitespace and an optional sign have defined handling, but the application's domain may forbid some of those forms. Parsing success is only the first step toward a valid value.
A fixed-size line buffer may receive only the first part of a longer input line. If complete lines matter, check for a newline and handle the remainder deliberately. The example uses a known in-memory string to focus on the conversion contract; a real input routine would add a bounded read first. This keeps the roles of reading, parsing, and domain validation separate.
Why the detail matters.
A line-oriented input design makes truncation visible. If fgets fills a buffer without seeing a newline, the rest of the line may remain unread; the next read could then be mistaken for a new command. The program can reject oversized lines or consume the remainder deliberately. This is an application policy, but the policy must be chosen. A numeric parser cannot repair a line-reading layer that silently splits one logical record into two.
strtol's result, end pointer, and errno answer different questions. The result is the converted number, end says which bytes were consumed, and ERANGE reports an out-of-range conversion. After that, the domain still needs to decide whether a negative number, zero, or leading whitespace is permitted. Separating these checks avoids confusing syntactic validity with a valid order quantity or user identifier. The example intentionally uses a known string so the conversion steps are easy to isolate.
See the rule in context.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char text[] = "42";
char *end = NULL;
long value = strtol(text, &end, 10);
if (end == text || *end != '\0') return 1;
printf("Value: %ld\n", value);
return 0;
}strtol reads the decimal digits 4 and 2, returning 42 as a long. It sets end to the terminating zero byte, so the two checks confirm that at least one digit was read and that no trailing text remains. The program displays Value: 42. For arbitrary input, it should also set errno to zero before the call and check ERANGE afterward.
Where the rule stops.
A successful conversion to long does not prove the number fits a smaller destination type or meets a domain rule such as nonnegative quantity. Validate before narrowing or using it.
Keep this model.
The same separation applies to files. A stream operation may read fewer bytes than requested, reach EOF, or report an error; file access adds resource lifetime and positioning to the story.