count = 0;
while ( (c = getchar()) != EOF)
{
if (c != '\t' && c != '\n') continue;
if (c == '\n') break;
if (c == '\t') count++;
}
what is the code mean and the getchar
and EOF
mean I did not understand
count = 0;
while ( (c = getchar()) != EOF)
{
if (c != '\t' && c != '\n') continue;
if (c == '\n') break;
if (c == '\t') count++;
}
what is the code mean and the getchar
and EOF
mean I did not understand
The getchar()
standard function reads a character from the standard input stream.
The returned value is of type int
(which is "wider" than char
) since it can also return EOF
for when input reading failed, generally because the stream was closed. The token EOF
in the code is simply a constant from the header, it can be declared like this:
#ifndef EOF
# define EOF (-1)
#endif
So it's literally just an alias for the integer literal -1
(but I don't think you can rely on the numerical value, use the symbolic name!).
The code loops until EOF
is detected, i.e. it loops over all characters readable on its standard input stream, and counts the number of times \t
is found.
It can be simplified a lot, the first if
is pointless (and the continue
is of course mis-spelled).