6月18日
#include <stdio.h>
(1)
/*count characters in input; 1st version*/
int main(void)
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld/n",nc);
return 0;
}
Note: when you Enter Backspace key, the machine would send the buffered string into input,
So 12345 counts 6.
(2)
#include <stdio.h>
/*count lines in input;*/
int main(void)
{
int c,nl;
nl = 0;
while ((c = getchar())!= EOF)
if (c == '/n')
++nl;
printf("%ld",nl);
}
Newcomers to C occasionally write = when they mean ==. The result is usually a legal expression, so you will get no warning.
A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set. This is called a character constant, although it is just another way to write a small integer. So, for example, 'A' is a character constant; in the ASCII character set its value is 65, the internal representation of the character A.
You should note carefully that '/n' is a single character, and in expressions is just an integer; on the other hand, “/n” is a string constant that happens to contain only one character.