Reputation: 3
I'm extremely new to C and need help with this question. I want to run through an array to check to see if all elements of the array are digits from 0-9. If they aren't, I want to print false and if they are, I want to print true. For some reason, my if statement is being skipped and what I have just returns false five times. Thanks!
#include <stdio.h>
int main()
{
int array[5] = {1, 2, 3, 15, 24};
int i;
for (i = 0; i < 5; i++) {
if (array[i] >= '0' && array[i] <= '9') {
printf("True\n");
}
else
{
printf("False\n");
}
}
}
Upvotes: 0
Views: 142
Reputation: 1188
Remove the ''
because you are not working with a string.
Instead, use this:
if (array[i] >= 0 && array[i] <= 9) {
Upvotes: 0
Reputation: 6437
None of the elements of the array are above '0', because '0' is 30 decimal in ASCII.
So with your example, the highest int being 24, none of the elements satisfy your if
condition.
Change it to :
if (array[i] >= 0 && array[i] <= 9) {
Upvotes: 3