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
5
votes
3 answers

Clarification in Section 5.10 of K&R 2

A pattern recognition program must print all lines containing the patter if the input is find pattern. If the input is find -x pattern, the program must print all lines except the lines containing pattern. // ..... switch(c) { case 'x': …
ThunderPunch
  • 483
  • 1
  • 4
  • 16
5
votes
2 answers

I don't understand the hash tables example in K&R

here is the install function from the hash tables example from K&R's book: struct nlist *install(char *name, char *defn) { struct nlist *np; unsigned hashval; if ((np = lookup(name)) == NULL) { /* not found */ np = (struct nlist…
Bar K
  • 89
  • 1
  • 7
5
votes
5 answers

Does this small C program satisfy the K&R exercise?

I'm on to K&R's Exercise 1-18 Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. This is what I've came up with so far #include #define MAXLINE 1000 int getline(char line[],…
alex
  • 479,566
  • 201
  • 878
  • 984
5
votes
1 answer

When left shift isn't the same as multiply by 2

I attempted KR exercise 2.1 - determine the range of long variables by direct calculation. #include #include int main() { unsigned short long_bits = sizeof( long ) * CHAR_BIT; printf( "LONG_MAX = %ld\n", ( 1L << (…
artm
  • 17,291
  • 6
  • 38
  • 54
5
votes
8 answers

How should I learn C?

I'm interested in learning C. I have read K & R, and I have even done some simple C extension work in R and Python. What's a worthwhile project idea for doing something more substantial with C? Any good online resources, similar to Dive Into…
Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
5
votes
3 answers

What function is K&R exercise 2-6 really asking for?

Exercise 2-6. Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged. I have been trying to mess around with this function on paper for a good…
Leonardo
  • 1,452
  • 3
  • 15
  • 26
5
votes
2 answers

Unsure about correctness of a sentence from K&R - pointer arithmetic | Freeing procedure

Quotation: The test if (allocbuf + ALLOCSIZE - allocp >= n) { checks if there's enough room to satisfy a request for n characters. If there is, the new value of allocp would be at most one beyond the end of allocbuf. The code which it…
Peter Cerba
  • 806
  • 4
  • 14
  • 26
4
votes
6 answers

Why is a function declared near the top of a source file?

I'm working through K & R to learn programming. Going well so far, but I'm unclear about the role of a line of code from section 1.8 (functions). In section 1.8, the authors show you how to create a function to raise one integer to the power of…
Graeme
  • 1,107
  • 2
  • 10
  • 11
4
votes
3 answers

The validity of casting in the 'function pointer' version of K&R's qsort

This question is about the 'function pointer' version of qsort from K&R (2e), section 5.11 (p118-121). There are a number of places where I don't understand why or how the casts work, and I suspect that the program is not entirely…
4
votes
3 answers

Is the memory allocation done by malloc always contiguous?

I have read in the K&R book that "the space that malloc manages may not be contiguous. each block contains size ,a pointer to next block.". But when i did google i saw that most of the people are saying that malloc always does contiguous memory …
4
votes
1 answer

Call by reference behavior when calling by value

I am working through chapter 1.9 of the K&R C book and I don't fully understand the example code given. In it, there is a function getline(line, MAXLINE) that returns an int of the length of a line. However, right afterwards, the 'line' variable is…
4
votes
3 answers

K&R Programming in C: EX 3.3

EX 3.3: Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9…
Fazer
  • 89
  • 11
4
votes
2 answers

Why do I get this error: Conflicting types for getline

Can somebody please take a look at this and tell me what is wrong. I have 3 errors: error: Conflicting types for getline error: too few arguments to function call, expected 3 have 2 error: conflicting types for getline. I'm sure I have overlooked…
MikeG
  • 3,745
  • 1
  • 29
  • 51
4
votes
1 answer

K&R 1-24. Why isn't my program properly checking for matching single and double quotes?

Unnecessarily complete source code: #include main() { int c; //char read from stdin. Type is int to accomodate EOF int x; //generic counter int nl_counter = 1; //new line counter int parens, brackets,…
JawiMmm
  • 61
  • 4
4
votes
2 answers

Where is the "space itself" in the storage allocator described at the end of K&R's book?

At the very end of Kernighan & Ritchie's book the The C Programming Language, a storage allocator is described. It says Each block contains a size, a pointer to the next block, and the space itself. But I don't see that in the code: typedef long…
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424