How to check if a character is digit in C

2 Answers

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

int main(void)
{
    if (isdigit('1'))
        printf("digit\n");
   
    if (isdigit('a'))
        printf("digit\n");
    else
        printf("NOT digit\n");

    return 0;
}

  
/*
  
run:
  
digit
NOT digit

*/

 



answered Nov 6, 2015 by avibootz
0 votes
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int x = 'b', y = '3';
    
    if (isdigit(x))
        printf("digit\n");
    else
        printf("NOT digit\n");
        
    if (isdigit(y))
        printf("digit\n");
    else
        printf("NOT digit\n");

    return 0;
}

  
/*
  
run:
  
NOT digit
digit

*/

 



answered Nov 6, 2015 by avibootz
...