C library - iswprint() function



The C wctype library iswprint() function is used to check if a given wide character (of type wint_t) can be printed or not.

The printable character includes either a number (0123456789), an uppercase letter (ABCDEFGHIJKLMNOPQRSTUVWXYZ), a lowercase letter (abcdefghijklmnopqrstuvwxyz), a punctuation character (!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~), space, or any printable character specific to the current C locale.

This function incudes all the graphical character and space character.

Syntax

Following is the C library syntax of iswprint() function −

int iswprint( wint_t ch );

Parameters

This function accepts a single parameter −

  • ch − It is a wide character of type 'wint_t' to be checked.

Return Value

This function returns Non-zero value if the wide character can be printed, zero otherwise.

Example 1

Following is the basic c program to demonstrate the use of iswprint().

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
   // Set the locale to "en_US.utf8" 
   setlocale(LC_ALL, "en_US.utf8");
   
   wchar_t space = L' ';
   
   // Check if the character is printable
   int res = iswprint(space);
   
   if (res) {
      printf("The character ' ' is a printable character.\n");
   } else {
      printf("The character ' ' is not a printable character.\n");
   }
   return 0;
}

Output

Following is the output −

The character ' ' is a printable character.

Example 2

Let's create another c example and use the iswprint() to identify printable characters in a wide character set.

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
   // "en_US.utf8" to handle Unicode characters
   setlocale(LC_ALL, "en_US.utf8");

   // Array of wide characters including
   // printable and non-printable characters
   wchar_t character[] = {L'A', L' ', L'\n', L'\u2028'};
   
   size_t num_char = sizeof(character) / sizeof(character[0]);

   // Check and print if a character is printable character or not
   printf("Checking printable characters:\n");
   for (size_t i = 0; i < num_char; ++i) {
      wchar_t ch = character[i];
      printf("Character '%lc' (%#x) is %s printable char\n", ch, ch, iswprint(ch) ? "a" : "not a");
   }
   
   return 0;
}

Output

Following is the output −

Checking printable characters:
Character 'A' (0x41) is a printable char
Character ' ' (0x20) is a printable char
Character '
' (0xa) is not a printable char
Character ' ' (0x2028) is not a printable char

Example 3

The below c example uses the iswprint() to the check if the special character is a printable character or not in the Unicode locale.

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
   // "en_US.utf8" to handle Unicode characters
   setlocale(LC_ALL, "en_US.utf8");

   wchar_t spclChar[] = {L'@', L'#', L'$', L'%'};
   
   size_t num_char = sizeof(spclChar) / sizeof(spclChar[0]);

   // Check and print if a character is printable character or not
   printf("Checking printable characters:\n");
   for (size_t i = 0; i < num_char; ++i) {
      wchar_t ch = spclChar[i];
      printf("Char '%lc' (%#x) is %s printable char\n", ch, ch, iswprint(ch) ? "a" : "not a");
   }
   
   return 0;
}

Output

Following is the output −

Checking printable characters:
Char '@' (0x40) is a printable char
Char '#' (0x23) is a printable char
Char '$' (0x24) is a printable char
Char '%' (0x25) is a printable char
c_library_wctype_h.htm
Advertisements