captain
captain

Reputation: 1827

Program not printing out numbers

I'm a beginner in C and I bought the book "The C programming language" and started reading and doing everything it says. There is a code in the book which should print out the number of lines,words and characters of a sentence. This is the code of the book.

#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;
      }
 }
printf("%d %d %d\n", nl, nw, nc);
} 

For some reason the values that should be printed out with the printf aren't shown. I really don't know what is wrong. It only prints out numbers if I put the printf in the while loop but this can't be right because it prints the numbers every time the values change.

Upvotes: 1

Views: 1416

Answers (5)

Akash Kumar Sharma
Akash Kumar Sharma

Reputation: 628

it was a equality issue as pointed above in if (c == ' ' || c == '\n' || c = '\t'). I would suggest you use a good IDE like Eclipse CDT which warns you of such bugs .

Upvotes: 0

thelionroars1337
thelionroars1337

Reputation: 198

Besides the typo others have mentioned, you need to send an End-Of-File signal to end the input loop. This will depend on the environment you are running it - will be Ctrl+Z in Windows, and should be Ctrl+D in *NIX environments. I needed to enter this on a new line and press Enter afterwards when using my usual IDE (Code::Blocks).

What it looks like

Remember too that at the time K&R (1 & 2) was written, command line programming was the norm, and typically you would expect the output to be visible if you just ended the program. Some IDEs will close their terminal emulator at the end of the program before you can view the results, so you may need to add something like

printf("\nPress return to continue");
getchar();

at the end of the program. Or, run it yourself from the OS terminal emulator (using cmd.exe on Windows, or gnome-terminal, yakuake, or whatever your OS provides). Navigate to the folder and

your_executable.exe

or

./your_executable

Upvotes: 0

Soren
Soren

Reputation: 14688

You have a typo in this line -- the assignment of c = '\t' always evaluates to true

if (c == ' ' || c == '\n' || c = '\t')

To fix, change = to ==

if (c == ' ' || c == '\n' || c == '\t')

To answer your question why does it not print anything out -- I think that the compiler is either giving your an error or warning causing the program not being linked, and hence you are not actually compiling the source code as listed, but rather you are running an old version of whatever code you compiled before.

Upvotes: 3

tomk
tomk

Reputation: 1356

It looks like you are reading your input from STDIN (the keyboard) and leave the loop only on "End of file", which will not happen. Do as the other answer suggests.

Upvotes: 1

marinara
marinara

Reputation: 538

gcc gives a compile error on this line:

if (c == ' ' || c == '\n' || c = '\t')

looking @ that line, it's easy to spot a typo: c = '\t')

missing an equality test, no? :)

Upvotes: 1

Related Questions