w3resource

C isdigit() function

C isdigit(int ch)

The isdigit() function is used to check whether a character is numeric character (0-9) or not. The function is defined in the ctype.h header file.

Numerical digit: A numerical digit (often shortened to just digit) is a single symbol used alone (such as "2") or in combinations (such as "25"), to represent numbers in a positional numeral system. The name "digit" comes from the fact that the ten digits (Latin digiti meaning fingers) of the hands correspond to the ten symbols of the common base 10 numeral system, i.e. the decimal (ancient Latin adjective decem meaning ten) digits.

Function Prototype of isdigit()

int isdigit( int arg );

isdigit() Parameters:

Name Description Required /Optional
ch ch is a character of class digit in the current locale. Required

Return value from isdigit()

  • The isdigit() function returns non-zero if ch is a decimal digit; otherwise, returns 0.

Example: C isdigit() function

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

int main()
{
    char ch;
    ch = '5';
    printf("\nIf %c is  digit character or not? %d", ch, isdigit(ch));
    ch='+';
    printf("\nIf %c is  digit character or not? %d", ch, isdigit(ch));
    ch = 'f';
    printf("\nIf %c is  digit character or not? %d", ch, isdigit(ch));
    return 0;
}

Output:

If 5 is  digit character or not? 1
If + is  digit character or not? 0
If f is  digit character or not? 0

Example: C Program to check a input character is numeric character or not

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

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;
}

Output:

Input a character: 7
7 is a digit.
----------------------- 
Input a character: A
A is not a digit.

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous C Programming: C iscntrl()
Next C Programming: C isgraph()



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M