Questions tagged [kernighan-and-ritchie]

Questions about or related to the book "The C Programming Language" (which is also known as K&R) by Brian Kernighan and Dennis Ritchie.

Questions about or related to the book "The C Programming Language" (which is also known as K&R) by Brian Kernighan and Dennis Ritchie.

The authors came together to write the book in conjunction with the language's early development at AT&T Bell Labs.

There have been two editions of the book. The first edition, commonly called "K&R1", was published in 1978 and describes a pre-standard version of the language. This edition is now mostly of historical interest.

The second edition, "K&R2", was first published in 1988 with some updates of the book to meet the version of the language standardized by ANSI in 1989.

(The 1989 ANSI C standard was republished by ISO in 1990 and a normative amendment was released in 1995. New versions of the standard were published in 1999 and 2011. There have been no new editions of K&R covering any of the changes to the language since 1990.)

335 questions
169
votes
1 answer

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

Today I reached page 167 of The C Programming Language (second edition Brian W. Kernighan & Dennis M. Ritchie) and found that the author says I must cast malloc. Here is the part from the book: 7.8.5 Storage Management The functions malloc and…
Michi
  • 5,175
  • 7
  • 33
  • 58
90
votes
13 answers

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

As mentioned in many of my previous questions, I'm working through K&R, and am currently into the preprocessor. One of the more interesting things — something I never knew before from any of my prior attempts to learn C — is the ## preprocessor…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
86
votes
6 answers

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

What is useful about this C syntax — using 'K&R' style function declarations? int func (p, p2) void* p; int p2; { return 0; } I was able to write this in Visual Studios 2010beta // yes, the arguments are flipped void f() { void* v…
75
votes
4 answers

How can a C compiler be written in C?

This question may stem from a misunderstanding of compilers on my part, but here goes... One can find the following statement in the preface to the first edition of K&R (page xi): The operating system, the C compiler, and essentially all UNIX…
jub0bs
  • 60,866
  • 25
  • 183
  • 186
59
votes
5 answers

How to simulate an EOF?

I am currently reading K&R's book and typing in the examples from the first section, and there are a couple of examples such as this: while((c = getchar()) != EOF) { //do something } I am testing these examples on a Windows box and thus running…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
55
votes
9 answers

'\0' evaluates false, "\0" evaluates true

Inspired by a program described in K&R section 5.5: void strcpy(char *s, char *t) { while(*s++ = *t++); } C program if ('\0') { printf("\'\\0\' -> true \n"); } else { printf("\'\\0\' -> false\n"); } if ("\0") { printf("\"\\0\" -> true…
Rahn
  • 4,787
  • 4
  • 31
  • 57
45
votes
7 answers

Why do I get a "conflicting types for getline" error when compiling the longest line example in chapter 1 of K&R2?

Here is a program I'm trying to run straight from section 1.9 of "The C Programming Language". #include #define MAXLINE 1000 int getline(char line[], int maxline); void copy(char to[], char from[]); main() { int len; int max; …
Salar
  • 453
  • 1
  • 4
  • 5
43
votes
4 answers

Signal EOF in mac osx terminal

I am stumped by the 1.5.2 question in K&R. I googled for sometime and found out that I have to supply the EOF input after entering the characters. long nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); return 0; I tried both…
Morpheus
  • 3,285
  • 4
  • 27
  • 57
40
votes
6 answers

What's a good example of register variable usage in C?

I'm reading through K&R and came to the small section on register variables, and was wondering if people here have some good examples of this put into practice. From section 4.7 in K&R: The register declaration looks like register int x; …
Kyle Walsh
  • 2,774
  • 4
  • 27
  • 25
39
votes
4 answers

Explain this implementation of malloc from the K&R book

This is an excerpt from the book on C by Kernighan and Ritchie. It shows how to implement a version of malloc. Although well commented, I am having great difficulty in understanding it. Can somebody please explain it? typedef long Align; /* for…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
34
votes
13 answers

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

I've been working through some books on C trying to get my C legs (sea-legs! Get it?!). I've just finished exercise 1-9 from the K&R book, which for reference is to "write a program to copy its input to its output, replacing each string of one or…
slaxx
  • 351
  • 3
  • 7
24
votes
5 answers

What's the escape sequence for blanks in C?

I'm writing a program to count blanks, tabs, and newlines. I remember what the escape sequence for tabs and newlines are, but what about blanks? \b? Or is that backspace?
MW2000
  • 553
  • 4
  • 7
  • 12
23
votes
7 answers

What does char * argv[] means?

I'm new to C programming, I encountered a problem. In case of complicated declarations i found this int *daytab[13]; // daytab is an array of 13 pointers to int which means daytab is the name of the array and the name of the array points to the…
akash
  • 1,801
  • 7
  • 24
  • 42
23
votes
4 answers

"extern" inside a function?

Well, reading "a bit old" book ("The C programming language", second edition, by Dennis Ritchie), I came a cross the following: An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
20
votes
6 answers

K&R Exercise 1.16 - Limitation on line length

I'm learning C from K&R's "The C Programming Language" book. I'm doing the exercises specified in the book. I'm on exercise number 1.16, but I don't understand it. Exercise 1.16: Revise the main routine of the longest-line program so it will …
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
1
2 3
22 23