Purpose

These functions return a non-zero (true) value if the argument is an ascii character in the indicated category:

Except for isascii, the return value is undefined if the argument is not an ascii character.  These functions may be implemented as macros.

Header File

ctype.h

Prototypes

int isalnum (int Value);
int isalpha (int Value);
int isascii (int Value);
int iscntrl (int Value);
int isdigit (int Value);
int isgraph (int Value);
int islower (int Value);
int isprint (int Value);
int ispunct (int Value);
int isspace (int Value);
int isupper (int Value);
int isxdigit (int Value);

Argument

Value
The character to be tested.

Return Values

0
The character is not in the category.
>0
The character is in the category.  The actual value returned is implementaion dependent.

Example

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

void Display (int (*isX) (int));

void Init ();  

char * D[128];

int main () {
  Init ();
  printf ("isalnum\n");
  Display (&isalnum);
  printf ("isalpha\n");
  Display (&isalpha);
  printf ("isascii\n");
  Display (&isascii);
  printf ("iscntrl\n");
  Display (&iscntrl);
  printf ("isdigit\n");
  Display (&isdigit);
  printf ("isgraph\n");
  Display (&isgraph);
  printf ("islower\n");
  Display (&islower);
  printf ("isprint\n");
  Display (&isprint);
  printf ("ispunct\n");
  Display (&ispunct);
  printf ("isspace\n");
  Display (&isspace);
  printf ("isupper\n");
  Display (&isupper);
  printf ("isxdigit\n");
  Display (&isxdigit);
  return 0;
}

void Display (int (*isX) (int)) {
  int i;
  int Begin;
  int Current;
  Begin = 0;
  Current = isX (0);
  i = 0;
  while (i < 128) {
    if ((isX (i) != Current) && (i > 1)) {
      printf ("  %s..%s: %i\n",  D[Begin], D[i - 1], Current);
      Begin = i;
      Current = isX (i);
    }
    ++i;
  }
  printf ("  %s..%s: %i\n",  D[Begin], D[i - 1], Current);
}

void Init () {
  int i;
  i = 0;
  while (i < 32) {
    D [i] = (char *) malloc (7);
    D [i][0] = '\'';
    D [i][1] = '\\';
    D [i][2] = '0' + ((i >> 6) & 7);
    D [i][3] = '0' + ((i >> 3) & 7);
    D [i][4] = '0' + (i & 7);
    D [i][5] = '\'';
    D [i][6] = 0;
    ++i;
  }
  while (i < 127) {
    D [i] = (char *) malloc (4);
    D [i][0] = '\'';
    D [i][1] = i;
    D [i][2] = '\'';
    D [i][3] = 0;
    ++i;
  }
  D[0] = "\'\\0\'";
  D['\\'] = "\'\\\\\'";
  D['\"'] = "\'\\\"\'";
  D['\''] = "\'\\\'\'";
  D['\?'] = "\'\\\?\'";
  D['\a'] = "\'\\a\'";
  D['\b'] = "\'\\b\'";
  D['\f'] = "\'\\f\'";
  D['\n'] = "\'\\n\'";
  D['\r'] = "\'\\r\'";
  D['\t'] = "\'\\t\'";
  D['\v'] = "\'\\v\'";
  D[127] = "\'\\177\'";
}

Output

isalnum
  '\0'..'/': 0
  '0'..'9': 4
  ':'..'@': 0
  'A'..'Z': 256
  '['..'`': 0
  'a'..'z': 256
  '{'..'\177': 0
isalpha
  '\0'..'@': 0
  'A'..'Z': 256
  '['..'`': 0
  'a'..'z': 256
  '{'..'\177': 0
isascii
  '\0'..'\177': 1
iscntrl
  '\0'..'\037': 32
  ' '..'~': 0
  '\177'..'\177': 32
isdigit
  '\0'..'/': 0
  '0'..'9': 4
  ':'..'\177': 0
isgraph
  '\0'..' ': 0
  '!'..'/': 16
  '0'..'9': 132
  ':'..'@': 16
  'A'..'F': 384
  'G'..'Z': 256
  '['..'`': 16
  'a'..'f': 384
  'g'..'z': 256
  '{'..'~': 16
  '\177'..'\177': 0
islower
  '\0'..'`': 0
  'a'..'z': 2
  '{'..'\177': 0
isprint
  '\0'..'\037': 0
  ' '..' ': 64
  '!'..'/': 16
  '0'..'9': 132
  ':'..'@': 16
  'A'..'F': 384
  'G'..'Z': 256
  '['..'`': 16
  'a'..'f': 384
  'g'..'z': 256
  '{'..'~': 16
  '\177'..'\177': 0
ispunct
  '\0'..' ': 0
  '!'..'/': 16
  '0'..'9': 0
  ':'..'@': 16
  'A'..'Z': 0
  '['..'`': 16
  'a'..'z': 0
  '{'..'~': 16
  '\177'..'\177': 0
isspace
  '\0'..'\b': 0
  '\t'..'\r': 8
  '\016'..'\037': 0
  ' '..' ': 8
  '!'..'\177': 0
isupper
  '\0'..'@': 0
  'A'..'Z': 1
  '['..'\177': 0
isxdigit
  '\0'..'/': 0
  '0'..'9': 128
  ':'..'@': 0
  'A'..'F': 128
  'G'..'`': 0
  'a'..'f': 128
  'g'..'\177': 0

See Also

ASCII   Computer Science Now

Valid HTML 4.01 Transitional

Valid CSS

Site Icon Copyright © 1999-, jhyoung, revised 00/00/0