When running the program provided in chapter 1.6 Arrays of The C Programming Language I get some very inconsistent results from the final printf.
Here is the program:
#include <stdio.h>
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 \n nother = %d \n", nwhite, nother);
}
Here's the execution: Windows PowerShell
I'm greatly confused by the apparent randomness of it. I'd be very grateful to anyone who can highlight what I'm missing here, especially considering this isn't even a program of my own making: this is provided by the book itself.
Thank you.
EDIT:
I've tried running the program in Command Prompt as opposed to Windows PowerShell. No difference in the results.
Also I've tried adding an int at the beginning of the main
int k = 99;
and two printf's, before and after, the final printf
printf("%d", k);
printf(" white space = %d \n nother = %d \n", nwhite, nother);
printf("%d", k);
Only the first one of the two extra printf's is printed but at the expense of 2 more characters from the middle one.