Recently, I was learning C programming just a hobby and followed the instruction inside of a book by B.K. & Dennis Ritchie 2nd edition. in many section I've just encountered about character input and output, up to 1.6 section which explains arrays also counting a special character in many ways. Before jumped into arrays section specifically 1.5.2 up to 1.5.4 that is explaining getchar() and putchar() usage. however, some cases had been finished and it returns its input to output and never terminate a.out after putting character that returns which means output can't capture an input into printf(). This is my code which is followed in the book.
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; ++i) {
ndigit[i] = 0;
}
while ((c = getchar()) != EOF){
if (c >= '0' && c <= '9'){
++ndigit[c-'0'];
} else if (c == ' ' || c == '\n' || c == '\t') {
++nwhite;
} else {++nother;}
}
printf("digits =");
for (i = 0; i < 10; ++i){
printf("%d", ndigit[i]);
} printf(", white space = %d, other = %d\n", nwhite, nother);
return 0;
}
the code is a part of arrays section example to count the number of occurrences of each digit, white space characters and all characters. but when I try it that's not returning a value which is included within printf()
my output:
$./a.out
for examplee
the output from my file can't be terminating its self except forcibly and doesn't take anything. its output should be like this.
digits = 9 3 0 0 0 0 0 0 0 1 white space = 123, other = 345