Comparison of isalnum of function and isalpha of function in C language

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

C isalnum() function: determines whether a character is an English letter or a number
The header file:


#include <ctype.h>

Isalnum () is used to determine whether a character is an English letter or a number, which is equivalent to isalpha(c) || isdigit(c). Its prototype is:


  int isalnum(int c);

[parameter] c is the character to be detected.

[return value] if c is a letter or a number, if c is 0 ~ 9   A to z   A ~ Z returns non-0, otherwise 0.

Note that isalnum() is defined for a macro, not a real function.

Example: find the characters in the STR string that are English letters or Numbers.


#include <ctype.h>
main(){
  char str[] = "123c@#FDsP[e?";
  int i;
  for (i = 0; str[i] != 0; i++)
    if(isalnum(str[i]))
      printf("%c is an alphanumeric charactern", str[i]);
}

Output results:


1 is an apphabetic character
2 is an apphabetic character
3 is an apphabetic character
c is an apphabetic character
F is an apphabetic character
D is an apphabetic character
s is an apphabetic character
P is an apphabetic character
e is an apphabetic character

C language isalpha() function: determine whether a character is an English letter
The header file:


#include <ctype.h>

Isalpha () is used to determine whether a character is an English letter. It is equivalent to isupper(c)||islower(c). Its prototype is:


  int isalpha(int c);

[parameter] c is the character to be detected.

[return value] if the parameter c is an English letter (a ~ z)   A ~ Z), returns A non-0 value, otherwise returns 0.

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

Example: find the English alphabetic characters in the STR string.


#include <ctype.h>
main(){
  char str[] = "123c@#FDsP[e?";
  int i;
  for (i = 0; str[i] != 0; i++)
    if(isalpha(str[i]))
      printf("%c is an alphanumeric charactern", str[i]);
}

Execution results:


c is an apphabetic character
F is an apphabetic character
D is an apphabetic character
s is an apphabetic character
P is an apphabetic character
e is an apphabetic character


Related articles: