typical newbie ?

Discussion in 'C Programming' started by NEW_to_C, Apr 21, 2005.

  1. NEW_to_C

    NEW_to_C Guest

    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;
    }
     
    NEW_to_C, Apr 21, 2005
    #1
    1. Advertising

  2. NEW_to_C <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 21, 2005
    #2
    1. Advertising

  3. 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
     
    E. Robert Tisdale, Apr 22, 2005
    #3
  4. 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
     
    E. Robert Tisdale, Apr 22, 2005
    #4
  5. "E. Robert Tisdale" <> 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) <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.
     
    Keith Thompson, Apr 22, 2005
    #5
  6. NEW_to_C

    Alex Fraser Guest

    "Keith Thompson" <> wrote in message
    news:...
    > NEW_to_C <> 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
     
    Alex Fraser, Apr 22, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick
    Replies:
    7
    Views:
    474
  2. happyrav

    typical entry level job

    happyrav, Jun 28, 2003, in forum: Java
    Replies:
    10
    Views:
    1,430
    Jon A. Cruz
    Jul 1, 2003
  3. NOBODY
    Replies:
    2
    Views:
    481
    NOBODY
    Feb 25, 2006
  4. Thomas Mlynarczyk

    Typical bad example wanted

    Thomas Mlynarczyk, Jan 22, 2005, in forum: HTML
    Replies:
    19
    Views:
    738
  5. Bulba!
    Replies:
    1
    Views:
    305
    Peter Otten
    Jan 6, 2005
Loading...

Share This Page