I am novice in c programming and have written the following program to calculate the number of blanks, tabs and new line characters in the input at the command prompt. I am using turbo c compiler and when i run this program i am getting correct output for number of tabs but for the other two the result is something which is very high and i can't understand. Please help.
Code:
#include<stdio.h>
main()
{
int nb,nt,nl;
int c;
while((c=getchar())!=EOF)
{
if(c == ' ')
nb++;
if(c == '\t')
nt++;
if(c == '\n')
nl++;
}
printf("blanks: %d\ntabs: %d\nnewlines: %d\n");
}
example run:
input:
this is the last
time i am attempting to run
this program before posting
output:
blanks:861
tabs: 4
newlines:230
The output for tabs is as expected since i have given 4 tab characters but the remaining two values is a bit confusing.
i have used ctrl+z to terminate the program if that makes any difference.