0

This is the code sample for the word counting program. But it's not working. When we execute it, after entering words it is supposed to display the results, but it is not producing anything.Anything missing in this code ?

#include<stdio.h>

#define IN  1 /* inside a word */
#define OUT 0 /* outside a word */

/* counts 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;
        }

    }
    printf("%d %d %d\n", nl, nw, nc);
}
Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
  • `But it's not working`..please elaborate. – Sourav Ghosh Jun 29 '15 at 07:55
  • 1
    You are not new around here, are you? Just in case, you've missed, let me humbly point you once again to take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) to learn what we expect from questions here. – Sourav Ghosh Jun 29 '15 at 07:56

1 Answers1

4

Your code is fine. You have to ask yourself how to break the while loop as it continuously reading input i.e. how to send EOF to your program.

On *nix systems, you do CTRL+D and on Windows you do CTRL+Z to generate EOF.

Also: use one of standard signature for main() such asint main(void).

P.P
  • 117,907
  • 20
  • 175
  • 238
  • Thanks for the answer. It works fine now. I have used main() since that was the code in that book. – Krishnadas PC Jun 29 '15 at 08:04
  • It's totally okay using `main()` You can call it as `int main(float a)` if you like. – Imobilis Jun 29 '15 at 08:08
  • 2
    @malina: No it isn't. `main()` goes back to the days were a missing return type for a function defaulted to `int`, which is no longer a guarantee. The standard also specifies there two (and only two) valid definitions of `main`: `int main ( void )` and `int main(int argc, char *argv[])` (C99: 5.1.2.2.1 Program startup) – Elias Van Ootegem Jun 29 '15 at 08:15
  • The return type has nothing to do with the arguments passed to int. Also even if it comes to using main, using `float` instead of `int` doesn't violate anything even with the full level of complaints. – Imobilis Jun 29 '15 at 08:17
  • 2
    @Malina: It does, check the standard: _"The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: `int main (void)` or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): `int main(int argc, char *argv[])` or equivalent; 10) or in some other implementation-defined manner."_ – Elias Van Ootegem Jun 29 '15 at 08:19
  • _"Equivalent"_ is specified in a footnote as follows: _"Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on."_. Any other `main` definition (ie returning a float for some reason) is _implementation defined_, and therefore non-standard and non-portable – Elias Van Ootegem Jun 29 '15 at 08:20
  • @EliasVanOotegem That doesn't change the thing. Even though it is not mentioned by standards, it doesn't mean it is "invalid". It will still run the program correctly, arguments are there only to interact with the cl. Your program wouldn't look correct if called as `main(float argc)` but standards < knowing exactly how main entry works. – Imobilis Jun 29 '15 at 08:20
  • 1
    @Malina: Check the last comment: _"implementation defined"_ means no guarantees that your code can be compiled correctly by another standard-compliant compiler. According to the standard, a compiler _may_ accept `float main (struct some_struct *foo)`, but that doesn't make it standard, and sure as hell doesn't make your code portable – Elias Van Ootegem Jun 29 '15 at 08:23
  • Well yes. gcc describes a number of constraint-optimizations. Would make me a terrible programmer if I relied on this. Just pointed out that this is not the issue. – Imobilis Jun 29 '15 at 08:24