Program to find out the data type of user input
Last Updated :
26 Dec, 2017
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 :
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
# define MAX_INPUT 100
int main()
{
char value[MAX_INPUT] = "" ;
double temp;
int n;
char str[MAX_INPUT] = "" ;
double val = 1e-12;
fgets (value, 100, stdin);
if ( sscanf (value, "%lf" , &temp) == 1)
{
n = ( int )temp;
if ( fabs (temp - n) / temp > val)
printf ( "The input is a floating point\n" );
else
printf ( "The input is an integer\n" );
}
else if ( sscanf (value, "%s" , str) == 1)
printf ( "The input is a string\n" );
else
printf ( "input not recognized\n" );
}
|
Output :
4
The input is an integer
Share your thoughts in the comments
Please Login to comment...