typical newbie ?

N

NEW_to_C

Could somebody please tell me what the done does in all this and what is
the last char for? I mean whar does it do? Can anybody give me a brief
overview on how this program actually works, I'm clear on some things
while other's are just plain murky.


#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;
}
done = 1;
}
lastchar = c;
}

printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
return 0;
}
 
K

Keith Thompson

NEW_to_C said:
Could somebody please tell me what the done does in all this and what
is the last char for? I mean whar does it do? Can anybody give me a
brief overview on how this program actually works, I'm clear on some
things while other's are just plain murky.


#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;
}
done = 1;
}
lastchar = c;
}

printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
return 0;
}

The varable done is being used as a flag. Since C90 doesn't have a
built-in boolean type (as some other languages do), the program uses
an int; the value 0 is false, and any non-zero value is treated as
true. (C99 has a built-in boolean type called _Bool, or bool if you
#include <stdbool.h>, but the newer standard isn't universally
implemented.)

See section 9 of the C FAQ, <http://www.eskimo.com/~scs/C-faq/top.html>,
for more (pre-C99) information on booleans in C.

IMHO, the condition should be "while (!done)" rather than
"while(done == 0)" to emphasize that it's testing a flag, not just
comparing an integer to 0. It's the same thing as far as the compiler
is concerned, but theres a conceptual difference.

The last block of code within the loop, starting with "if(c == EOF)",
increments newlines again if the last character before EOF was not a
newline. In other words, a file containing 3 newline characters that
doesn't end in a newline will be counted as having 4 newlines. (I
find this questionable, but just an odd defintion of "newlines", not
necessarily an error.)
 
E

E. Robert Tisdale

NEW_to_C said:
Could somebody please tell me what the done does in all this?
And what is the last char for? I mean what does it do?
Can anybody give me a brief overview on how this program actually works?
I'm clear on some things while other's are just plain murky.
> cat main.c
#include <stdio.h>
#include <stdbool.h>

int main(int argc, char* argv[]) {

int blanks = 0;
int tabs = 0;
int newlines = 0;

bool done = false;
while (!done) {
int lastchar = 0;
int c = getchar();

if (c == EOF) {
if (lastchar != '\n') {
++newlines;
}
done = true;
break;
}
else
if (c == ' ')
++blanks;
else
if (c == '\t')
++tabs;
else
if (c == '\n')
++newlines;

lastchar = c;
}

printf("Blanks: %d\nTabs: %d\nLines: %d\n",
blanks, tabs, newlines);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main < main.c
Blanks: 177
Tabs: 5
Lines: 40
 
E

E. Robert Tisdale

E. Robert Tisdale said:
NEW_to_C said:
Could somebody please tell me what the done does in all this?
And what is the last char for? I mean what does it do?
Can anybody give me a brief overview on how this program actually works?
I'm clear on some things while other's are just plain murky.

cat main.c
#include <stdio.h>
#include <stdbool.h>

int main(int argc, char* argv[]) {

int blanks = 0;
int tabs = 0;
int newlines = 0;
int lastchar = 0;
 
K

Keith Thompson

E. Robert Tisdale said:
NEW_to_C said:
Could somebody please tell me what the done does in all this?
And what is the last char for? I mean what does it do?
Can anybody give me a brief overview on how this program actually works?
I'm clear on some things while other's are just plain murky.
cat main.c [snip]
gcc -Wall -std=c99 -pedantic -o main main.c
./main < main.c
Blanks: 177
Tabs: 5
Lines: 40

I suspect ERT actually thinks he's being helpful. To the original
poster: If you don't find his followup illuminating, feel free to
ignore it.

Note that ERT's program uses <stdbool.h>, a new header in C99 (which
is not yet universally supported). If your compiler supports it,
that's great. If not, see section 9 of the C FAQ for alternatives.
 
A

Alex Fraser

^^^^^
[snip]
The last block of code within the loop, starting with "if(c == EOF)",
increments newlines again if the last character before EOF was not a
newline. In other words, a file containing 3 newline characters that
doesn't end in a newline will be counted as having 4 newlines. (I
find this questionable, but just an odd defintion of "newlines", not
necessarily an error.)

Until EOF is reached, newlines stores the number of newline characters seen
so far. When EOF is reached, it becomes the number of lines instead (which
is one more than the number of newlines if and only if the character before
EOF was not a newline).

Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top