isprint() Function
The isprint()
function in C checks whether a character is printable, meaning it determines if a character occupies a printing position on a display. This function is useful for filtering out control characters and ensuring that data intended for display is visible.
Syntax of isprint()
int isprint(int c);
Parameters
Parameter | Description |
---|---|
c | An integer representing the character to be checked, typically cast from a char or EOF. |
It is noteworthy that for the standard ASCII character set in the default “C” locale, a character is considered printable if its ASCII code is greater than 0x1F and is not equal to 0x7F. Additionally, while isprint()
returns true for the space character, the isgraph()
function does not.
Return Value
The function returns a nonzero value (true) if the character is printable, and zero (false) otherwise.
Examples for isprint()
Example 1: Basic Check for a Printable Character
This example demonstrates the basic usage of isprint()
by checking a single character to determine if it is printable.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = ' ';
if (isprint(ch)) {
printf("The character %c is printable.\n", ch);
} else {
printf("The character %c is not printable.\n", ch);
}
return 0;
}
Explanation:
- A character is assigned the value
' '
(space). - The
isprint()
function checks whether this character is printable. - Since the space character is considered printable, the program prints a confirmation message.
Program Output:
The character is printable.
Example 2: Checking a Non-Printable Control Character
This example illustrates how isprint()
can be used to detect a control character that is not printable.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
int ch = '\n';
if (isprint(ch)) {
printf("The character (newline) is printable.\n");
} else {
printf("The character (newline) is not printable.\n");
}
return 0;
}
Explanation:
- A character is assigned the newline character
'\n'
. - The
isprint()
function checks if the newline character is printable. - Since control characters like newline are not considered printable, the function returns false, and the corresponding message is printed.
Program Output:
The character (newline) is not printable.
Example 3: Iterating Through a String to Check Printability
This example demonstrates how to iterate through a string and use isprint()
to verify the printability of each character.
Program
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello,\nWorld!";
int i = 0;
while (str[i] != '\0') {
if (isprint(str[i])) {
printf("Character '%c' is printable.\n", str[i]);
} else {
printf("Character (non-printable) detected.\n");
}
i++;
}
return 0;
}
Explanation:
- A string
"Hello,\nWorld!"
is defined, containing both printable characters and a newline control character. - The program iterates over each character in the string.
- The
isprint()
function checks each character’s printability. - Printable characters result in a message stating they are printable, while non-printable characters trigger a different message.
Program Output:
Character 'H' is printable.
Character 'e' is printable.
Character 'l' is printable.
Character 'l' is printable.
Character 'o' is printable.
Character ',' is printable.
Character (non-printable) detected.
Character 'W' is printable.
Character 'o' is printable.
Character 'r' is printable.
Character 'l' is printable.
Character 'd' is printable.
Character '!' is printable.