Program to find out the data type of user input

Take a input from user and find out the data type of input value.

Examples :

Input : geek
Output : The input is a string

Input : chetna
Output : The input is a string

Input : 4
Output : The input is a integer

Below is C program to find the datatype of user input :



filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find data type
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
# define MAX_INPUT 100
  
int main()
{
    // To read input
    char value[MAX_INPUT] = "";
  
    // To store numeric value of input if a 
    // number (float or integer)
    double temp;
  
    // To store integral value of input
    int n;
  
    // To store string value of input
    char str[MAX_INPUT] = "";
  
    // Precision for integer checking
    double val = 1e-12;
  
    fgets(value, 100, stdin); // Read input
  
    // Check for integers.
    if (sscanf(value, "%lf", &temp) == 1) 
    {
        n = (int)temp; // typecast to int.
        if (fabs(temp - n) / temp > val) 
            printf("The input is a floating point\n");        
        else 
            printf("The input is an integer\n");        
    }
  
    // Check for string 
    else if (sscanf(value, "%s", str) == 1)     
        printf("The input is a string\n");
      
    else // No match.    
        printf("input not recognized\n");    
}

chevron_right


Output :

4
The input is an integer


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.