I have had this book "The C Programming Language Second Edition" (Spanish Version) for a few years, I recently decided i would give it a shot. It would be my first programming language and yes I know It's not easy for begginers, i like challenges. Thing is luckily i found a not-working correctly example (see below).
The following code is supposed to count and display the count everytime nc is modified. thing is printf is actually not printing anything. Changing EOF to 1 and typing it won't end the program either.
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%开发者_高级运维ld\n", nc);
}
Question: Should i be aware of any "Recent" changes in C?. This book is from 1991, 20 years... (wow I'm getting old)
If you want to print after each iteration you have to surround the while with brackets.
while(getchar() != EOF)
{
++nc;
printf("%ld\n", nc);
}
If you want to "break" the while (send EOF) you need CTRL-D or CTRL-Z.
Anyway, if the book doesn't specify brackets, indent it like this:
while(getchar() != EOF)
++nc;
printf("%ld\n", nc);
You may want to add a few brakets:
while(getchar() != EOF) {
++nc;
printf("%ld\n", nc);
}
This will print the value of nc each time you type a character.
cnictuar's suggestion is exactly what that code snippet looks like in my English paperback edition, 17th printing. Maybe the indent got screwed up in the Spanish translation, but it feels like a funny thing to change.
There are a lot of new features in C99, but the ones that I'm really happy about are:
named initializers:
struct {int a, b, c, d;} s = { .a = 1, .c = 3, 4, .b = 5};
declarations in
for
loops:for (int i; i<foo; i++) { ... }
Initializers for auto aggregates can be non-constant expressions:
void foo(int n) { int arr[n]; }
精彩评论