K&R Exercise 1-9: output the input, replacing multiple blanks by a single blank

In the for-loop statement, you’re having the bug. for(ch = getchar(); (ch = getchar()) != EOF; lch = ch){…} Here, you’re storing first character in ch, and then again testing if (ch!=EOF) by again reading character input. Remove ch=getchar() from the initialisation statement; let it be in the second part. for(;(ch = getchar()) != EOF; … Read more

Signal EOF in mac osx terminal

By default, macOS (formerly OS X and Mac OS X) software recognizes EOF when Control-D is pressed at the beginning of a line. (I believe this behavior is similar for other versions of Unix as well.) In detail, the actual operation is that, when Control-D is pressed, all bytes in the terminal’s input buffer are … Read more

How can a C compiler be written in C? [duplicate]

It’s called Bootstrapping, quoting from Wikipedia: If one needs a compiler for language X to obtain a compiler for language X (which is written in language X), how did the first compiler get written? Possible methods to solving this chicken or the egg problem include: Implementing an interpreter or compiler for language X in language … Read more

Alternative (K&R) C syntax for function declaration versus prototypes

The question you are asking is really two questions, not one. Most replies so far tried to cover the entire thing with a generic blanket “this is K&R style” answer, while in fact only a small part of it has anything to do with what is known as K&R style (unless you see the entire … Read more

What are the applications of the ## preprocessor operator and gotchas to consider?

One thing to be aware of when you’re using the token-paste (‘##‘) or stringizing (‘#‘) preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases. If you don’t do this and the items passed to the token-pasting operator are macros themselves, you’ll get results … Read more

Why does “The C Programming Language” book say I must cast malloc?

From http://computer-programming-forum.com/47-c-language/a9c4a586c7dcd3fe.htm: In pre-ANSI C — as described in K&R-1 — malloc() returned a char * and it was necessary to cast its return value in all cases where the receiving variable was not also a char *. The new void * type in Standard C makes these contortions unnecessary. To save anybody from the … Read more