In C programming, the isprint() function is a standard library function defined in the <ctype.h> header file. It is used to determine whether a given character is a printable character or not. The function takes an integer argument (usually the ASCII value of a character) and returns an integer value indicating whether the character is printable or not.
The isprint() function has the following syntax:
<span class="hljs-type">int</span> <span class="hljs-title function_">isprint</span><span class="hljs-params">(<span class="hljs-type">int</span> ch)</span>;
The ch parameter represents the character to be tested. It can be an integer value or a character literal.
The isprint() function returns a non-zero value if the character is a printable character, and it returns 0 (zero) otherwise. Printable characters include alphabets (A-Z and a-z), digits (0-9), punctuation marks, symbols, and whitespace characters (space, tab, newline, etc.). Non-printable characters include control characters (ASCII values less than 32) and other special characters that do not represent visible characters.
Here’s an example usage of isprint():
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><ctype.h></span></span>
<span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span> {
<span class="hljs-type">char</span> ch = <span class="hljs-string">'A'</span>;
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">isprint</span>(ch)) {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c is a printable character.\n"</span>, ch);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c is not a printable character.\n"</span>, ch);
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
In this example, the isprint() function is used to check if the character ‘A’ is printable. Since ‘A’ is indeed a printable character, the program will output: “A is a printable character.”
