1

I am a beginner of C language, and now trying to count the number of characters which is input.

#include <stdio.h>

main()
{
    long nc;
    nc = 0;
    while (getchar() != EOF)
        ++nc;
    printf("%1d\n", nc);
}

This is what I wrote just as my textbook writes, but printf function doesn't seem to work.

In addition, this program doesn't seem to finish, because the prompt is not coming up.

I have no idea if the contents of this book are old enough.

Could you tell me what is wrong with this code?

agongji
  • 117
  • 1
  • 7
  • 2
    This looks like code from K&R. And there was someone else who ran into the same problem of misinterpreting `%ld` as `%1d`. Given that `nc` is of type `long`, you need `%ld` (letter ell). The book contains an ell and not a one. The use of `main()` shows that the book is dated (1988 for the 2nd Edn). C99 requires a return type and prefers `void` in the argument list: `int main(void)`. – Jonathan Leffler Feb 15 '20 at 07:56
  • 1
    Hello close-voters, I recommend to NOT consider this question a typo. John has ingeniously explained a much different problem from point of view of OP. – Yunnosch Feb 15 '20 at 08:05
  • 1
    The previous question was [No output display for the codes written as per C Programming Language by Brian W Kernighan and Dennis M Ritchie](https://stackoverflow.com/q/60228292/15168), but it is closed now. That one had some extra typos in it. – Jonathan Leffler Feb 15 '20 at 08:05
  • 3
    If you enable warnings (which you should always do) the compiler will point out the problem for you, e.g. "warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’". With GCC you can use the -Wall flag. – August Karlstrom Feb 15 '20 at 08:13

1 Answers1

3

This looks like code from K&R The C Programming Language, 2nd Edn (1988), Chapter 1, p18.

The problem is that your transcription of the code is misinterpreting %ld as %1d. Given that nc is of type long, you need %ld (letter ell) and not %1d (digit one). The book contains an ell and not a one.

With suitable options, compilers like GCC and Clang will give warnings about type mismatches in the format strings. Use -Wall -Werror to get errors when the code is malformed (or -Wformat if you can't work with -Wall — but I use -Wall -Wextra -Werror plus a few extra fussier options for all my compilations; I won't risk making mistakes that the compiler can tell me about).

The use of main() shows that the book is dated. C99 requires a return type and prefers void in the argument list — int main(void) — when you don't make use of the command line arguments.

As to the program not finishing, when you're typing at the terminal, you indicate EOF (end of file) to the program by typing Control-D on most Unix-like systems (though it is configurable), and Control-Z on Windows systems. (If you want to indicate EOF without typing a newline immediately beforehand, you need to type the EOF indicator twice instead of just once.) Or you can feed it a file from the shell: counter < data-file (assuming the program is called counter and you want to count the characters in the file data-file).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • As you have guessed, this is from K&R. And I have fixed some type mismatches which you pointed out, and besides I have now understood what EOF really means. when I had returned to prompt, I had alway used Contrl-C, that's why printf fuction hadn't worked well. Thank you very much for your advice which was really correct. It is my first time to make use of this site, and I am now satifsfied. – agongji Feb 17 '20 at 07:42