C Program to Read and Print an Integer
C Program to Read and Print an Integer
A student enters a count into a classroom program. The program must distinguish a number from text that merely starts with digits.
Convert a bounded input line with strtol, reject conversion or trailing-text failures, and check the allowed range before storing an int.
Before this exercise, review storing values and reading and checking input. The worked program below applies those ideas to this specific problem.
Explain
Explain why read an integer uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Try 7students and explain why it must not print Count: 7.
Read an Integer: follow the data
Given data
A student enters a count into a classroom program. The program must distinguish a number from text that merely starts with digits.
Rule
Convert a bounded input line with strtol, reject conversion or trailing-text failures, and check the allowed range before storing an int.
Observed result
Count: 7
Understand the operation
This program reuses the 0–30 classroom-count contract from validated C input. Reading seven followed by a newline is valid, but seven followed by students is not the same answer.
strtol sets an end pointer after the parsed digits. Skipping whitespace is permitted here; accepting other leftover characters would silently ignore malformed input.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
static int parse_count(const char *line, int *count) {
char *end;
errno = 0;
long value = strtol(line, &end, 10);
if (end == line || errno == ERANGE) return 0;
while (isspace((unsigned char)*end)) end++;
if (*end != '\0' || value < 0 || value > 30) return 0;
*count = (int)value;
return 1;
}
int main(void) {
char line[64];
int count;
if (fgets(line, sizeof line, stdin) == NULL || (strchr(line, '\n') == NULL && !feof(stdin)) || !parse_count(line, &count)) {
puts("Enter a whole count from 0 to 30");
return 0;
}
printf("Count: %d\n", count);
return 0;
}
Example input
7
Expected output
Count: 7
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
static int parse_count(const char *line, int *count) {
char *end;
errno = 0;
long value = strtol(line, &end, 10);
if (end == line || errno == ERANGE) return 0;
while (isspace((unsigned char)*end)) end++;
if (*end != '\0' || value < 0 || value > 30) return 0;
*count = (int)value;
return 1;
}
int main(void) {
char line[64];
int count;
if (fgets(line, sizeof line, stdin) == NULL || (strchr(line, '\n') == NULL && !feof(stdin)) || !parse_count(line, &count)) {
puts("Enter a whole count from 0 to 30");
return 0;
}
printf("Count: %d\n", count);
return 0;
}
The sample line contains seven and a line ending.
The parser accepts its digits, validates the end and range, and stores seven for the fixed %d output conversion.
Follow the worked calculation
Start with the sample
The sample line contains seven and a line ending.
Apply the rule
The parser accepts its digits, validates the end and range, and stores seven for the fixed %d output conversion.
Check the result
Count: 7
Count: 7
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
static int parse_count(const char *line, int *count) {
char *end;
errno = 0;
long value = strtol(line, &end, 10);
if (end == line || errno == ERANGE) return 0;
while (isspace((unsigned char)*end)) end++;
if (*end != '\0' || value < 0 || value > 30) return 0;
*count = (int)value;
return 1;
}
int main(void) {
char line[64];
int count;
if (fgets(line, sizeof line, stdin) == NULL || (strchr(line, '\n') == NULL && !feof(stdin)) || !parse_count(line, &count)) {
puts("Enter a whole count from 0 to 30");
return 0;
}
printf("Count: %d\n", count);
return 0;
}
Try 7students and explain why it must not print Count: 7.
Worked answer
The remaining suffix violates the whole-line numeric contract. The program rejects it before writing an accepted count; parsing an initial prefix alone is not sufficient.
C17 · complete program
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
static int parse_count(const char *line, int *count) {
char *end;
errno = 0;
long value = strtol(line, &end, 10);
if (end == line || errno == ERANGE) return 0;
while (isspace((unsigned char)*end)) end++;
if (*end != '\0' || value < 0 || value > 30) return 0;
*count = (int)value;
return 1;
}
int main(void) {
char line[64];
int count;
if (fgets(line, sizeof line, stdin) == NULL || (strchr(line, '\n') == NULL && !feof(stdin)) || !parse_count(line, &count)) {
puts("Enter a whole count from 0 to 30");
return 0;
}
printf("Count: %d\n", count);
return 0;
}
Example input
7students
Expected output
Enter a whole count from 0 to 30
Run in C compiler →Recognize the important boundaries
The lesson deliberately chooses a 0–30 domain, not every mathematical integer. Change both the checked limits and destination type if the application needs a wider domain.
Check errno for range failure. Do not assume a successful-looking decimal prefix fits the eventual destination type.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Convert a bounded input line with strtol, reject conversion or trailing-text failures, and check the allowed range before storing an int.
Continue with testing the problem’s boundary cases.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.