I'm trying to test this simple look up table, but can't understand why both scanf
and getchar
add the newline character ('\n'
) to the input:
const int arr[10] = {1,0,5,7,6,4,8,2,9,3};
char digit;
printf("enter digits please\n");
digit = getchar();
while ((digit>='0') && (digit<='9'))
{
printf("%d --> %d\n",digit,arr[digit-'0']);
digit = getchar();
}
printf("bye bye!");
when runnnig this code, both with printf
or getchar()
the while loop executes just once, since the newline char ('\n'
) is also stored in the char variable digit
, & I can't figure out why.