La funzione isdigit () controlla se un carattere è un carattere numerico (0-9) o meno.
Prototipo di funzione di isdigit ()
int isdigit (int arg);
La funzione isdigit () accetta un singolo argomento sotto forma di numero intero e restituisce il valore di tipo int
.
Anche se isdigit () accetta integer come argomento, il carattere viene passato alla funzione. Internamente, il carattere viene convertito nel suo valore ASCII per il controllo.
È definito nel file di intestazione "> file di intestazione.
C isdigit () Valore restituito
Valore di ritorno | Osservazioni |
---|---|
Numero intero diverso da zero (x> 0) | L'argomento è un carattere numerico. |
Zero (0) | L'argomento non è un carattere numerico. |
Esempio: funzione C isdigit ()
#include #include int main() ( char c; c='5'; printf("Result when numeric character is passed: %d", isdigit(c)); c='+'; printf("Result when non-numeric character is passed: %d", isdigit(c)); return 0; )
Produzione
Risultato quando viene passato un carattere numerico: 1 Risultato quando viene passato un carattere non numerico: 0
Esempio: programma C per verificare se un carattere immesso dall'utente è un carattere numerico o meno
#include #include int main() ( char c; printf("Enter a character: "); scanf("%c",&c); if (isdigit(c) == 0) printf("%c is not a digit.",c); else printf("%c is a digit.",c); return 0; )
Produzione
Immettere un carattere: 8 8 è una cifra.