C isprint() Function

The isprint() function in C is a standard library function that checks if a given character is printable. It is part of the C standard library (ctype.h). This function is useful for determining if a character is a printable character, including letters, digits, punctuation, and space.

Table of Contents

  1. Introduction
  2. isprint() Function Syntax
  3. Examples
    • Checking if a Character is Printable
    • Using isprint() with User Input
  4. Real-World Use Case
  5. Conclusion

Introduction

The isprint() function checks if a given character is printable, meaning it is a character that occupies a printing position on a display. This includes all letters, digits, punctuation marks, and the space character.

isprint() Function Syntax

The syntax for the isprint() function is as follows:

#include <ctype.h>
int isprint(int c);

Parameters:

  • c: The character to be checked, which is passed as an int.

Returns:

  • The function returns a non-zero value (true) if the character is printable; otherwise, it returns 0 (false).

Examples

Checking if a Character is Printable

To demonstrate how to use isprint() to check if a character is printable, we will write a simple program.

Example

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'A';

    // Check if the character is printable
    if (isprint(ch)) {
        printf("'%c' is a printable character.\n", ch);
    } else {
        printf("'%c' is not a printable character.\n", ch);
    }

    return 0;
}

Output:

'A' is a printable character.

Using isprint() with User Input

This example shows how to use isprint() to check if a character provided by the user is printable.

Example

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch;

    // Get user input for the character
    printf("Enter a character: ");
    scanf("%c", &ch);

    // Check if the character is printable
    if (isprint(ch)) {
        printf("'%c' is a printable character.\n", ch);
    } else {
        printf("'%c' is not a printable character.\n", ch);
    }

    return 0;
}

Output (example user input ' '):

Enter a character:
' ' is a printable character.

Output (example user input '\n'):

Enter a character:

'\n' is not a printable character.

Real-World Use Case

Validating Printable Characters in a String

In real-world applications, the isprint() function can be used to validate strings, ensuring that only printable characters are included.

Example: Filtering Non-Printable Characters

#include <stdio.h>
#include <ctype.h>

int main() {
    char input[100];
    int i;

    // Get user input for the string
    printf("Enter a string: ");
    fgets(input, sizeof(input), stdin);

    // Filter and print only printable characters
    printf("Printable characters in the input: ");
    for (i = 0; input[i] != '\0'; i++) {
        if (isprint(input[i])) {
            putchar(input[i]);
        }
    }
    printf("\n");

    return 0;
}

Output (example user input "Hello\nWorld!"):

Enter a string: Hello
World!
Printable characters in the input: HelloWorld!

Conclusion

The isprint() function is essential for checking if a character is printable in C. It is useful in various applications, particularly in fields like data validation and text processing, where it is necessary to ensure that characters are suitable for display or printing.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare