新手请教一个简单程序
工作需要,重学C。在看 The C Programming Language
这是其中一个练习给出的答案,有地方没看懂,请达人指点。
Write a program to count blanks, tabs, and newlines.
#include <stdio.h>
int main(void)
{
int blanks, tabs, newlines;
int c;
int done = 0;
int lastchar = 0;
blanks = 0;
tabs = 0;
newlines = 0;
while(done == 0)
{
c = getchar();
if(c == ' ')
++blanks;
if(c == '\t')
++tabs;
if(c == '\n')
++newlines;
if(c == EOF)
{
if(lastchar != '\n')
{
++newlines; /* this is a bit of a semantic stretch, but it copes
* with implementations where a text file might not
* end with a newline. Thanks to Jim Stad for pointing
* this out.
*/
}
done = 1;
}
lastchar = c;
}
printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
return 0;
}
-------------------
1. done=0,dong=1是指什么?
2. 可不可以给说明一下最后部分
if(c == EOF)
{
if(lastchar != '\n')
{
++newlines;
}
done = 1;
}
lastchar = c;
}
3.return 0是返回什么?
也许问题幼稚,没办法,身边没人请教,全得自己来。
多谢了!