C Function Isprint
# C Library Function - isprint()
The `isprint()` function is a built-in C library function used to check if a given character is a **printable character**. A printable character is any character that occupies a printing position on a display or page, which includes alphanumeric characters, punctuation marks, and the space character (`' '`). It excludes control characters (such as `\n`, `\t`, or `\b`).
This function is declared in the `` header file.
---
## Syntax
```c
int isprint(int c);
```
### Parameters
* **`c`**: This is the character to be checked. Although the parameter is of type `int`, it is passed as an unsigned char cast to an int, or `EOF` (End of File).
### Return Value
* **Non-zero value (True)**: If the character `c` is a printable character.
* **Zero (False)**: If the character `c` is not a printable character (e.g., control characters).
---
## Character Classification (ASCII)
In the standard ASCII character set, printable characters are those with decimal values ranging from **32 (space)** to **126 (tilde `~`)**.
* **Printable Characters (32 to 126)**: ` `, `!`, `"`, `#`, `$`, ..., `a`-`z`, `A`-`Z`, `0`-`9`, `~`.
* **Control Characters (0 to 31, and 127)**: `\t` (tab), `\n` (newline), `\0` (null terminator), `DEL` (delete), etc.
### Difference between `isprint()` and `isgraph()`
* `isprint()` considers the space character (`' '`, ASCII 32) to be a printable character.
* `isgraph()` behaves similarly but returns false (0) for the space character.
---
## Code Example
The following program demonstrates how to use the `isprint()` function to check different types of characters.
```c
#include
#include
int main()
{
int var1 = 'k';
int var2 = '8';
int var3 = '\t'; // Control character (Tab)
int var4 = ' '; // Space character
if (isprint(var1))
{
printf("var1 = |%c| is printable\n", var1);
}
else
{
printf("var1 = |%c| is not printable\n", var1);
}
if (isprint(var2))
{
printf("var2 = |%c| is printable\n", var2);
}
else
{
printf("var2 = |%c| is not printable\n", var2);
}
if (isprint(var3))
{
printf("var3 = |%c| is printable\n", var3);
}
else
{
printf("var3 = Control character (\\t) is not printable\n");
}
if (isprint(var4))
{
printf("var4 = |%c| is printable\n", var4);
}
else
{
printf("var4 = |%c| is not printable\n", var4);
}
return 0;
}
```
### Output
```text
var1 = |k| is printable
var2 = |8| is printable
var3 = Control character (\t) is not printable
var4 = | | is printable
```
---
## Practical Application: Filtering Control Characters
Below is a practical example showing how to use `isprint()` to sanitize a string by replacing or ignoring non-printable control characters.
```c
#include
#include
int main()
{
char raw_str[] = "Hello\nWorld!\tWelcome.";
printf("Original string with control characters:\n");
printf("%s\n\n", raw_str);
printf("Sanitized string (non-printable characters removed):\n");
for (int i = 0; raw_str != '\0'; i++)
{
if (isprint((unsigned char)raw_str))
{
putchar(raw_str);
}
}
printf("\n");
return 0;
}
```
### Output
```text
Original string with control characters:
Hello
World! Welcome.
Sanitized string (non-printable characters removed):
HelloWorld!Welcome.
```
---
## Considerations
1. **Undefined Behavior**: The behavior of `isprint()` is undefined if the argument `c` is not representable as an `unsigned char` or is not equal to `EOF`. To safely pass arbitrary `char` values, always cast the argument to `unsigned char`:
```c
isprint((unsigned char)c);
```
2. **Locale Dependency**: The behavior of `isprint()` depends on the current locale category `LC_CTYPE`. In the default `"C"` locale, only characters between ASCII 32 and 126 are printable. Other locales may define additional printable characters.
YouTip