Results 1 to 2 of 2
Hey Guys,
This is my first post and it is a very basic one. I hope to be an active member of this community as I learn more about The ...
- 08-13-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 1
Wrong Output for C Program
Hey Guys,
This is my first post and it is a very basic one. I hope to be an active member of this community as I learn more about The C programming language and Linux.
Here is my problem:
Using Text: The C Programming Language (2nd Ed.)
I'm not getting the correct output when I compile this code. It is supposed to display the number for each nw, nl, and nc. When I feed the program the EOF (Ctrl + D), it only displays the amount of times I entered anything. (For Example, I enter "Hi", "Hello", " ", "TAB", "ENTER"; This will display 5). Below is the code (directly from the book), any help appreciated. Thanks
I apologize for the sloppy formatting when I pasted it.Code:#include <stdio.h> #define IN 1 /*inside a word*/ #define OUT 0 /*outside a word*/ /*count lines, words, and characters in input*/ main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while((c = getchar()) != EOF) { ++nc; if(c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } //end while printf("%d %d %d \n", nl, nw, nc); }
-xDaedalusx
- 08-14-2010 #2
Just tried the code and it works...
Make mine Arch Linux


Reply With Quote