C++ isprint()

The isprint() function in C++ checks if the given character is printable or not.

isprint() Prototype

int isprint(int ch);

The isprint() function checks if ch is printable as classified by the current C locale. By default, the following characters are printable:

  • Digits (0 to 9)
  • Uppercase letters (A to Z)
  • Lowercase letters (a to z)
  • Punctuation characters (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)
  • Space

The behaviour of isprint() is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.

It is defined in <cctype> header file.

isprint() Parameters

ch: The character to check.

isprint() Return value

The isprint() function returns non zero value if ch is printable, otherwise returns zero.

Example: How isprint() function works

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

int main()
    char str[] = "Hello\tall\nhow are you";

    for (int i=0; i<strlen(str); i++)
        /* replace all non printable character by space */
        if (!isprint(str[i]))
            str[i] = ' ';

    cout << str;
    return 0;

When you run the program, the output will be:

Hello all how are you