2

Some of the examples in K&R don't work in Code:Blocks when I type them exactly. For example, this program:

#include <stdio.h>
main()
{
    long nc;
    nc = 0;
    while (getchar() != EOF)
        ++nc;
    printf("%ld\n", nc);
}

When I type this code and run it, the program either freezes or doesn't do anything when I press enter.

The program below does the same thing (count characters in a string) and it works.

#include <stdio.h>
int main()
{
    char s[1000];
    int i;
    scanf("%s",s);
    for(i=0; s[i]!='\0'; ++i);
    printf("Length of string: %d",i);
    return 0;
}

Am I missing something here? Has C been changed since K&R 2nd Edition or am I doing something wrong?

  • 7
    You need to press CTRL+D (Unix) or CTRL+Z (Windows) in order to receive `EOF`, take a look to [End-of-file](http://en.wikipedia.org/wiki/End-of-file) – David Ranieri Apr 20 '15 at 11:24
  • That worked. Thank you, I couldn't have thought of that. – user4010936 Apr 20 '15 at 11:26
  • 2
    Apart from that, C has changed a lot since K&R. You shouldn't be reading that book to learn the language, I'd recommend finding something modern and up-to-date instead. For example, this code won't compile on C99/C11 compilers because the declaration of main() is incorrect. – Lundin Apr 20 '15 at 11:29
  • What do you recommend? Is [C Primer Plus by Stephen Prata](http://www.amazon.com/gp/product/0321928423/ref=pd_lpo_sbs_dp_ss_3?pf_rd_p=1944687542&pf_rd_s=lpo-top-stripe-1&pf_rd_t=201&pf_rd_i=0321776402&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=0KM2FB194G895KNJ2STE) good? – user4010936 Apr 20 '15 at 11:43
  • Those programs really don't "do the same". – unwind Apr 20 '15 at 12:04
  • 1
    @Lundin And what material exactly in K&R isn't correct today? It's still one of the best descriptions of ISO 9899 I know. – fuz Apr 20 '15 at 12:27
  • @unwind As far as I can tell, they use very different methods to get the same result so my wording is wrong. – user4010936 Apr 20 '15 at 13:02
  • @FUZxxl Thank you for your comment. I am half way through this book and it is only 200 pages so I will just finish it. I don't want to be switching books at this point. – user4010936 Apr 20 '15 at 13:09
  • @user4010936 K&R is almost completely devoid of errors but it doesn't describe any C99 or C11 extensions because these language revisions didn't exist back then. Keep that in mind. – fuz Apr 20 '15 at 13:12
  • @FUZxxl Nonsense. The official errata, which is very forgiving, is several pages long. Apart from that, add the many cases of reliance on poorly-specified behavior. Then add all the errors because the book is unaware of C99 and C11. That's the factual errors. Then on top of that are the subjective errors: add the many cases of poor style, bad programming practice, dangerous programming practice. Check the code in K&R against a widely-recognized programming standard such as CERT C or MISRA C and see for yourself. – Lundin Apr 20 '15 at 13:45
  • @user4010936 I haven't read any beginner books about C so I don't have any recommendations, just a big anti-recommendation for K&R. Prata made it into the [SO book recommendations](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), for what it is worth. (I've read Prata's C++ book long time ago and wouldn't really recommend that one.) – Lundin Apr 20 '15 at 13:52

2 Answers2

6

When you press enter, you send \n into the standard input stream(and flushes other data into the stdin,if any). This character(\n) is not the same as EOF. To send EOF, press following keys combinations:

  • CTRL Z and then Enter in Windows.
  • CTRL D in Unix/Linux/OSX
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
2

C has been changed since that book was written, but even still the code is not well-defined/valid C under any version of the standard.

If you compile using the current C standard (C11), or the previous (C99), a strictly conforming compiler (such as gcc -std=c11 -pedantic-errors) will give an error: main must return int. The old "implicit int" feature was removed in C99.

If you compile using the old C90 standard, the code invokes undefined behavior, because there is no return statement. This was allowed in C90 but it is not safe, it could cause program crashes etc. Invoking undefined behavior is always a bug, and therefore very bad practice.

(In C99, functions returning a value must have a return statement, with the exception of main(), where return can be safely omitted. So you could omit the return statement of main() if you compile as C99.)

Simply correct the code as you have already done, and it will compile in any version of the standard, without invoking undefined behavior.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • All you say is true, but none of it answers the question asked. – Peter Apr 20 '15 at 14:41
  • @Peter The question was "Am I missing something here? Has C been changed since K&R 2nd Edition or am I doing something wrong?" How does it not answer that question? (The question in the title doesn't make any sense since Codeblocks is an IDE) This answer could very well explain why the program freezes, since the K&R code invokes undefined behavior. Although it does indeed seem far more likely that the OP simply didn't know how to feed a EOF to the console. The answer by Cool Guy is good and should most likely be picked as the correct one. I just mentioned additional issues in the code itself. – Lundin Apr 20 '15 at 14:43