0

I am new to coding and I am learning through a book called "The C Programming Language - 2nd Edition - Ritchie Kernighan" and there is this code:

#include<stdio.h>
#include<stdlib.h>
int main(){
    int c,nl;
    nl =0;
    while((c=getchar())!=EOF)
        if(c == '\n')
            ++nl;
    printf("%d\n",nl);
    return 0;
}

After typing the code in CodeBlocks I run it and when I type in a word and press enter nothing happens. The word is not being counted and printed. I am new to all of this but if anyone has an idea feel free to share it. Thank you very much !

user2736738
  • 30,591
  • 5
  • 42
  • 56
S.Sivchev
  • 17
  • 1
  • 4
    [don't put code in images](https://meta.stackoverflow.com/q/303812/995714) – phuclv Dec 24 '17 at 16:22
  • 1
    IT is the most widely spread misconception that this book is good for learning. It was at the time when it was the only one, when I started learning C, and that was in 80s. There are much better books for learning C. It does not mean that those smart guys who invented C are smart enough to explain how to learn it. Instead, they just said that you have to learn by programming, while giving examples that sometimes cannot compile. Search for another book, and use K&R book as a reference. It is a good reference book. – VladP Dec 24 '17 at 17:57
  • [The Definitive C Book Guide and List](https://stackoverflow.com/q/562303/995714) – phuclv Dec 25 '17 at 06:44

2 Answers2

3

The issue is that you never read the EOF (End Of File); this is the end of the input data coming from the console (where you type).

Everything you type is either a letter, digit, special character, or newline, but never EOF.

To generate an EOF you need to enter a special control-key combination. On Windows this is Ctrl+Z and on UNIX/Linux/macOS this is Ctrl+D.

The book you're reading is great and written by the two creators of C. It one of my first programming books and I still have it; all worn-out.

Small piece of advice: Always put your code blocks inside { } to avoid mistakes and create more visual clarity, use spaces consistently, and add empty lines. Your code would look like this:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int c, nl;
    nl = 0;

    while ((c = getchar()) != EOF)
    {
        if (c == '\n')
        {
            ++nl;
        }
    }

    printf("%d\n", nl);

    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
meaning-matters
  • 21,929
  • 10
  • 82
  • 142
1

Why should it stop? Your expectations are wrong. getchar() will go on getting characters until it encounters EOF.

How to pass that to getchar?

For Windows Ctrl+Z will do the trick. And then press Enter.
For Unix or Linux system it would be Ctrl+D

Responsive output before entering EOF

To get a more responsive output you can add this line, that will tell you the cumulative sum of \n found.

    if(c == '\n'){
        ++nl;
        printf("Till now %d newline found",nl);
        fflush(stdout);
    }

Further Explanation of the added code

The code segment provided above will provide you some output when you press enter. But the thing is until you enter EOF it will keep on waiting for more and more input. That's what also happened in the first case. So you have to press Ctrl+Z and press Enter. That will break the loop. And you will see the final output - the number of line count.

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56