isprint() function in c++
Introduction
C++ is a popular programming language used in various industries, including software development, game development, and finance. One of the essential functions in C++ is the "isprint()" function, which is used to check if a character is printable or not. In this article, we will look at the "isprint()" function in C++, how it works, and where it can be used.
Understanding the isprint() function in C++
The isprint() function is used to determine whether a character is printable or not. A printable character is any character that can be printed on the screen, such as letters, numbers, punctuation marks, and symbols. Non-printable characters include control characters, such as tabs, line feeds, and carriage returns.
The isprint() function is part of the library in C++. This library provides a collection of functions for character classification, including functions for checking whether a character is a digit, a letter, a punctuation mark, and so on. The isprint() function specifically checks whether a character is printable or not.
To use the isprint() function, you must include the header file in your program. Here's an example of how to use the isprint() function:
#include<iostream>
#include <cctype>
int main() {
char ch = 'A';
if (isprint(ch)) {
std::cout << "The character is printable.\n";
} else {
std::cout << "The character is not printable.\n";
}
return 0;
}
Explanation
- This program demonstrates the use of the isprint() function in C++. It checks whether a given character is printable or not.
- The program starts with including the necessary header file, which provides the function isprint(). The main function initializes a character variable 'ch' with the value 'A'.
- The program then uses an if-else statement to check whether the character 'ch' is printable or not using the isprint() function.
- If the isprint() function returns a non-zero value, the character is printable, and the program prints the message "The character is printable".
- If the isprint() function returns zero, the character is not printable, and the program prints the message "The character is not printable".
- In this program, the character 'A' is printable, so the program will print the message "The character is printable".
- However, if we change the value of 'ch' to a non-printable character, such as a control character, the program will print the message "The character is not printable".
Program Output

Syntax of the isprint() function
The syntax of the isprint() function is straightforward. It takes a single argument of type int, which represents the character, to be checked. The function returns an integer value that is non-zero if the character is printable, and zero if the character is not printable. Here's the syntax of the isprint() function:
int isprint(int c);
The argument c represents the character to be checked, and its type must be int. If the value of c is a printable character, the function returns a non-zero value. If the value of c is a non-printable character, the function returns zero.
Example program using the isprint() function
Let's take a closer look at how the isprint() function can be used in a program. Consider the following example:
#include<iostream>
#include <cctype>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::getline(std::cin, input);
std::string printable = "";
std::string non_printable = "";
for (char c : input) {
if (isprint(c)) {
printable += c;
} else {
non_printable += c;
}
}
std::cout << "Printable characters: " << printable << "\n";
std::cout << "Non-printable characters: " << non_printable
<< "\n";
return 0;
}
Explanation
- This program demonstrates the use of the isprint() function in C++ to check for printable characters in a user input string.
- The program starts with including the necessary header file, which provides the function isprint(). The main function initializes a string variable 'input' to store the user input.
- The program then uses std::getline() function to get a line of input from the user and store it in the 'input' variable.
- The program then initializes two string variables, 'printable' and 'non_printable', to store the printable and non-printable characters, respectively.
- The program then uses a for loop to iterate through each character in the 'input' string.
- Inside the for loop, the program uses the isprint() function to check whether the current character is printable or not.
- If the character is printable, the program appends it to the 'printable' string. If the character is not printable, the program appends it to the 'non_printable' string.
- Finally, the program prints out the two strings, separating the printable and non-printable characters.
Program Output

Applications of the isprint() function
The isprint() function can be used in various applications, including text processing, input validation, and data cleaning. In text processing, the isprint() function can be used to filter out non-printable characters from a text file or a user input. This can be useful for cleaning up the input and removing unwanted characters.
In input validation, the isprint() function can be used to check whether a user input contains only printable characters. This can help prevent security vulnerabilities such as buffer overflow attacks, where a user can enter non-printable characters to execute malicious code.
In data cleaning, the isprint() function can be used to filter out non-printable characters from a dataset. This can be useful for data analysis and machine learning applications, where non-printable characters can cause errors and inaccuracies in the data.
Conclusion
The isprint() function in C++ is an essential function for character classification. It can be used to check whether a character is printable or not, which is useful for text processing, input validation, and data cleaning.
The isprint() function is part of the <cctype> library, which provides a collection of functions for character classification. By using these functions, developers can ensure that their programs handle user input correctly and securely.