Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if the input is a number or not in C?

Tags:

c

types

In the main function of C:

void main(int argc, char **argv)
{
   // do something here
}

In the command line, we will type any number for example 1 or 2 as input, but it will be treated as char array for the parameter of argv, but how to make sure the input is a number, in case people typed hello or c?

like image 552
user2131316 Avatar asked Jun 25 '13 08:06

user2131316


People also ask

How to check if input is number or string in C++?

In this approach we can check input is number or string by converting the input to the int type. If an input is an integer, then it can successfully get converted to int, and we can say that entered input is number. Otherwise, You get a valueError exception and we can say that entered user input is a string. Program :

How to check if user input is number or string in JavaScript?

Use string isdigit () method to check user input is number or string Note: The isdigit () function will work only for positive integer numbers. i.e., if you pass any float number, it will not work. So, It is better to use the first approach. Let’s execute the program to validate this.

How to check if an input is an integer or float?

To check if the input string is an integer number, convert the user input to the integer type using the int () constructor. To check if the input is a float number, convert the user input to the float type using the float () constructor. If an input is an integer or float number, it can successfully get converted to int or float type.

How to check if user input is a number in Python?

The input () function always converts the user input into a string and then returns it to the calling program. Let us understand this with an example. As you can see, The output shows the type of a variable as a string (str). Solution: In such a situation, We need to convert user input explicitly to integer and float to check if it’s a number.


5 Answers

Another way of doing it is by using isdigit function. Below is the code for it:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXINPUT 100
int main()
{
    char input[MAXINPUT] = "";
    int length,i; 

    scanf ("%s", input);
    length = strlen (input);
    for (i=0;i<length; i++)
        if (!isdigit(input[i]))
        {
            printf ("Entered input is not a number\n");
            exit(1);
        }
    printf ("Given input is a number\n");
}
like image 131
Kranthi Kumar Avatar answered Oct 16 '22 12:10

Kranthi Kumar


You can use a function like strtol() which will convert a character array to a long.

It has a parameter which is a way to detect the first character that didn't convert properly. If this is anything other than the end of the string, then you have a problem.

See the following program for an example:

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

int main( int argc, char *argv[]) {
    int i;
    long val;
    char *next;

    // Process each argument given.

    for (i = 1; i < argc; i++) {
        // Get value with failure detection.

        val = strtol (argv[i], &next, 10);

        // Check for empty string and characters left after conversion.

        if ((next == argv[i]) || (*next != '\0')) {
            printf ("'%s' is not valid\n", argv[i]);
        } else {
            printf ("'%s' gives %ld\n", argv[i], val);
        }
    }

    return 0;
}

Running this, you can see it in operation:

pax> testprog hello "" 42 12.2 77x

'hello' is not valid
'' is not valid
'42' gives 42
'12.2' is not valid
'77x' is not valid
like image 30
paxdiablo Avatar answered Oct 16 '22 12:10

paxdiablo


Using scanf is very easy, this is an example :

if (scanf("%d", &val_a_tester) == 1) {
    ... // it's an integer
}
like image 27
Adrien.C Avatar answered Oct 16 '22 12:10

Adrien.C


A self-made solution:

bool isNumeric(const char *str) 
{
    while(*str != '\0')
    {
        if(*str < '0' || *str > '9')
            return false;
        str++;
    }
    return true;
}

Note that this solution should not be used in production-code, because it has severe limitations. But I like it for understanding C-Strings and ASCII.

like image 40
Marius Avatar answered Oct 16 '22 11:10

Marius


Using fairly simple code:

int i;
int value;
int n;
char ch;

/* Skip i==0 because that will be the program name */
for (i=1; i<argc; i++) {
    n = sscanf(argv[i], "%d%c", &value, &ch);

    if (n != 1) {
        /* sscanf didn't find a number to convert, so it wasn't a number */
    }
    else {
        /* It was */
    }
}
like image 2
Neil Townsend Avatar answered Oct 16 '22 13:10

Neil Townsend