You need to get the types of your variables right. An unsigned int
can only ever be that: an non-negative integer. You cannot look at the first character in an integer.
Your program analyses a string, i.e. an array of characters, even if it only looks at the first letter. Therefore, your data must be an array of characters. This also means that you need to scan for a string with "%s"
.
When you test the first character of your string, you must use an array index, str[0]
.
To put all this together:
#include <stdio.h>
int main()
{
char str[20];
printf("Please input a positive number: ");
scanf("%19s", str);
if ((str[0] >= 'a' && str[0] <= 'z') || (str[0] >='A' && str[0] <= 'Z')) {
printf("alpha\n");
} else if (str[0] >= '0' && str[0] <= '9') {
printf("integer\n");
} else {
printf("other\n");
}
return 0;
}
Edit: As pointed out, this code does not check whether a string repesents a decimal number. It only checks the first character. A full solution should check all characters and is left as an exercise to the original poster.
My impression is that the OP has difficulties to distinguish between the data types, so I tried to focus on that.
I've also added a field width to the scanf
to avoid buffer overflow. I'm not going into the details of sscanf
here, because I think that the OP still has to understand more of the very basic stuff.