ISDigit

The problem with the following code is that both the if and else statements get executed for every case.

#include<stdio.h>
#include<ctype.h>

int main()
{
char x;
while(1)
{
printf("\nenter the no to be printed\n");
scanf("%c",&x);


if(isdigit(x))

printf("A number");

else

printf(" Not a number");


}
return 0;



}

> both the if and else statements get executed for every case.
Look the output closely (input is prefixed with '> ')

enter the no to be printed
> a
 Not a number
enter the no to be printed
 Not a number
enter the no to be printed
> 4
A number
enter the no to be printed
 Not a number
enter the no to be printed
note that it executes the else case, but in another iteration of the loop (it display the "enter the no..." message).
You are processing the line break
Topic archived. No new replies allowed.