在本教程中,我们将讨论一个程序来找出用户输入的数据类型。
为此,我们将获得一些随机输入值。我们的任务是处理该输入并打印回给定输入的数据类型。
#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; //typecasting
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");
}输出结果
Input not recognized