|||

Learn To Solve It

Quick search

  • A Tutorial Introduction
  • Types, Operators and Expressions
  • Control Flow
  • Functions and Program Structure
  • Pointers and Arrays
  • Structures
  • Input and Output
    • 7.1 upper case to lower or lower case to upper
    • 7.2 print non-graphic characters in octal or hexadecimal
    • 7.3 minprintf to handle facilities of printf
    • 7.4 private version of scanf
    • 7.5 Postfix calculator using scanf
    • 7.6 Compare Two files
    • 7.7 Pattern matching program with files
    • 7.8 Print Pages to Files
    • 7.9 Analyze implementations of isupper
  • The UNIX System Interface
  • C Programming Building Blocks
  • C Concepts Visualization

7.2 print non-graphic characters in octal or hexadecimal¶

Question¶

Write a program that will print arbitrary input in a sensible way. As a minimum, it should print non-graphic characters in octal or hexadecimal according to local custom, and break long text lines.

/**
 * Write a program that will print arbitrary input in a sensible way. As a
 * minimum, it should print non-graphic characters in octal or hexadecimal
 * according to local custom, and break long text lines.
 */

#include <ctype.h>
#include <stdio.h>

#define MAXLINE 100 /* maximum number of chars in one line */
#define OCTLEN 6    /* length of an octal value */

const char *input = "This\tis\ta\ttest.   With a   very   long line.";
int input_index = 0;

int _getchar(void) {
    if (input[input_index] == '\0') {
        return EOF;
    } else {
        return input[input_index++];
    }
}


/* inc : increment position counter for output */
int inc(int pos, int n) {
    if (pos + n < MAXLINE)
        return pos + n;
    else {
        putchar('\n');
        return n;
    }
}

/* print arbitrary input in a sensible way */
int main(void) {
    int c, pos;

    pos = 0; /* position in the line */

    while ((c = _getchar()) != EOF)
        if (iscntrl(c) || c == ' ') {
            /* non-graphic or blank character */
            pos = inc(pos, OCTLEN);
            printf("\\%03o", c);
            /* newline character */
            if (c == '\n')
                pos = 0;
            putchar('\n');
        } else {
            /* graphic character */
            pos = inc(pos, 1);
            putchar(c);
        }
    return 0;
}

Explanation¶

We use the standard library function iscntrl declared in ctype.h to determine if a character is a control character. We keep track of the position to print the output in the inc function and print the output in the putchar function. If there is special character, we allocate 6 characters to it and print the character in octal along with the special character.

Visualize¶

Visualize the Concept¶

<Page contents

>Page contents:

  • 7.2 print non-graphic characters in octal or hexadecimal
    • Question
    • Explanation
    • Visualize
      • Visualize the Concept
<7.1 upper case to lower or lower case to upper
7.3 minprintf to handle facilities of printf>
© Copyright 2026 Senthil Kumaran. Last updated on 25 May, 26.

Styled using the Piccolo Theme