A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
isprint returns nonzero for any character that occupies a printing position on a display. In the C locale, that is ASCII 32 (space) through 126 (tilde). It includes letters, digits, punctuation, and the space character. It excludes control characters like newline, tab, and escape. Use it to sanitize output, to decide whether to display a character or show a placeholder, and to validate that a buffer contains displayable text.
#include <stdio.h>#include <ctype.h>void safe_print(const char *label, const unsigned char *data, size_t len) {printf("%s: ", label);for (size_t i = 0; i < len; i++) {if (isprint(data[i])) {putchar(data[i]);} else {printf("\\x%02X", data[i]);}}printf("\n");}int main(void) {unsigned char mixed[] = {'H', 'i', 0x07, 0x1B, '!', 0x00, 'A'};safe_print("Mixed data", mixed, sizeof(mixed));const char *clean = "Hello, World!";safe_print("Clean text", (const unsigned char *)clean, 13);return 0;}
The safe_print function displays printable characters directly and shows non-printable characters as hex escapes. The bell character (0x07), escape (0x1B), and null (0x00) are rendered as \x07, \x1B, and \x00 instead of corrupting the terminal output. This is the pattern used by hexdump, network debuggers, and log sanitizers.
isprint is the output sanitizer. Before displaying data from a file, network, or user input, check that it is printable. Replace or escape what is not. Your terminal and your users will thank you. Next: iscntrl, which detects the control characters isprint rejects.
Finish the lesson once you have worked through the material. This awards ★ 10 XP.