Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > typical newbie ?

Reply
Thread Tools

typical newbie ?

 
 
NEW_to_C
Guest
Posts: n/a
 
      04-21-2005
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;
}
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-21-2005
NEW_to_C <(E-Mail Removed)> writes:
> 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.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      04-21-2005
NEW_to_C wrote:

> 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
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      04-21-2005
E. Robert Tisdale wrote:

> NEW_to_C wrote:
>
>> 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;
>
> bool done = false;
> while (!done) {
> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-22-2005
"E. Robert Tisdale" <(E-Mail Removed)> writes:
> NEW_to_C wrote:
>
>> 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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Alex Fraser
Guest
Posts: n/a
 
      04-22-2005
"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> NEW_to_C <(E-Mail Removed)> writes:

[snip]
> > printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);

^^^^^
[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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dataware house typical file formats and extraction api? vs java capabilities NOBODY Java 2 02-25-2006 04:02 PM
How does typical ISP traffic shaping/bandwidth limiting work ? Do ISP's allow bursty traffic per second ? Skybuck Flying Cisco 0 01-19-2006 08:50 PM
Slow 54Mbps G network - what are typical speeds? Chris Wireless Networking 8 06-30-2004 04:58 AM
Are these stress test results typical? Rick ASP .Net 7 01-07-2004 04:26 PM
typical entry level job happyrav Java 10 07-01-2003 03:11 PM



Advertisments