📜  程序找出用户输入的数据类型

📅  最后修改于: 2021-05-28 04:35:00             🧑  作者: Mango

从用户那里获取输入,并找出输入值的数据类型。

例子 :

Input : geek
Output : The input is a string

Input : chetna
Output : The input is a string

Input : 4
Output : The input is a integer

下面是C程序,用于查找用户输入的数据类型:

// C program to find data type
#include 
#include 
#include 
# 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");    
}

输出 :

4
The input is an integer

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。