C language to determine whether a character is a printable character

  • 2020-04-02 03:16:51
  • OfStack

C language isprint() function: determine whether a character is printable
The header file:


#include <ctype.h>

The isprint() function is used to determine whether a character is a print character, and its prototype is:


  int isprint(int c);

[parameter] c is the character to be detected.

If c is a printable character, a non-zero value is returned, otherwise 0 is returned.

Printable characters with ASCII code values greater than 0x1f (except 0x7f(DEL)) can be displayed on the screen for us to see; Can't show on the screen, we can't see, called the control character, ASCII code value 0x00 ~ 0x1f, plus 0x7f(DEL). Use the isiscntrl() function to detect control characters.

Note that this function is a macro definition, not a real function.

Example: determines which printable characters in the STR string contain space characters.


#include <ctype.h>
main(){
  char str[] = "a5 @;";
  int i;
  for(i = 0; str[i] != 0; i++)
    if(isprint(str[i]))
      printf("str[%d] is printable character:%dn", i, str[i]);
}

Output results:


str[0] is printable character:a
str[1] is printable character:5
str[2] is printable character:
str[3] is printable character:@
str[4] is printable character:;

C isgraph() function: determines whether a character is printable except for a space
The header file:


#include <ctype.h>

Isgraph () is used to determine whether a character is printable except for Spaces. Its prototype is:


  int isgraph (int c);

[parameter] c is the character to be detected.

If the ASCII code corresponding to c is printable and is a non-space character, a non-zero value is returned, otherwise 0 is returned.

Note that isgraph() is defined as a macro, not a real function.

Example: determines which printable characters are in the STR string.


#include <ctype.h>
main(){
  char str[] = "a5 @;";
  int i;
  for(i = 0; str[i] != 0; i++)
    if(isgraph(str[i]))
      printf("str[%d] is printable character:%dn", i, str[i]);
}

Output results:


str[0] is printable character:a
str[1] is printable character:5
str[3] is printable character:@
str[4] is printable character:;


Related articles: