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

K&R - Numerical sort of alpha characters?

What does it mean to numerically sort alpha characters in opposite of lexicographic, like in K&R 5-14 with option -n
highlevelcoder
  • 345
  • 1
  • 3
  • 10
3
votes
1 answer

Return value from writing an unused parameter when falling off the end of a non-void function

In this golfing answer I saw a trick where the return value is the second parameter which is not passed in. int f(i, j) { j = i; } int main() { return f(3); } From gcc's assembly output it looks like when the code copies j = i it…
qwr
  • 9,525
  • 5
  • 58
  • 102
3
votes
1 answer

The longest string

I'm going through K&R C programming language book and try to solve all the exercises. There is a sample program that finds the longest string in the input. It basically reads strings from input one by one and store the longest in the array with…
ivan
  • 311
  • 1
  • 4
  • 13
3
votes
2 answers

"the C programming language" book exercise 1.2 possible wrong result

#include /* print Fahrenheit-Celsius table for fahr = 0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; upper = 300; step = 20; fahr = lower; while (fahr <=…
Ravenous
  • 356
  • 1
  • 4
  • 16
3
votes
4 answers

K&R exercise: Multidimensional array into pointer array

Exercise (5-9): Rewrite the routines day_of_year with pointers instead of indexing. static char daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; /* day_of_year:…
Tyler
  • 4,679
  • 12
  • 41
  • 60
3
votes
3 answers

Is K&R teaching bad readability?

It has been a while since I looked at C (still learning) and I just got back into the K&R book. I just had a go to Exercise 5-3 (p107). Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to…
alex
  • 479,566
  • 201
  • 878
  • 984
3
votes
1 answer

K&R book exercise 4-2

I'm studying K&R book. I'm currently at chapter 4. I was reading the atof() function on page 71. Function atof(s) converts string to its double precision floating point equivalent. The code of atof() is as following: //atof: convert string s to…
Hussein Barada
  • 117
  • 1
  • 11
3
votes
2 answers

Cannot find a solution to K&R exercise 4-6

In K&R we have managed to create an RPN. The exercise now is to: Add commands for handling variables, (It's easy to provide twenty-six variables with single letter names.) Add a variable for the most recently printed value. So this is meant to…
lamenuts
  • 31
  • 1
3
votes
2 answers

Standard libraries included multiple times over several files?

In the K&R book (p59) (edit: second edition, covering ANSI C), it is suggested that it is easier to split larger projects into multiple files. In each file, several libraries are included at the top as usual: e.g. getop.c needs stdio.h, and so does…
3
votes
1 answer

The C Programming Language (K&R) exercise 2-8: Rotate a number to the right. Is this OK?

I'm following The C Programming Language (K&R). This is exercise 2-8. It says to create a function to rotate a number to the right by some number of bits. The answer I came up with 'seems' to do the job, and in two lines. However, I was checking for…
Somjit
  • 2,503
  • 5
  • 33
  • 60
3
votes
2 answers

K&R Exercise 2-7, optimisations?

I'm currently learning C with "The C Programming Language" from K&R. I solved the exercise 2-7, which says: Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed into 0 and vice versa),…
GilDev
  • 514
  • 5
  • 14
3
votes
5 answers

K&R Exercise 1-20 The Programming Language 2nd edition

What should i do in this program. I cant understand. The question is as : Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns.…
Abhishek
  • 79
  • 1
  • 7
3
votes
1 answer

What are these last lines of code doing in Exercise 1-13 of K & R's The C Programming Language?

I am very new to programming in general, so please bear with my lack of knowledge. I have spent a couple of hours now on exercise 1-13. I finally decided to look up the answer, which I found at this link…
jhschwartz
  • 168
  • 1
  • 2
  • 13
3
votes
1 answer

Kernighan & Ritchie malloc free logic

I have spent hours on one particular condition in free() implementation. I have searched the web and stackoverflow to see if anyone else discussed this, but i found none. I understand the general idea behind the 3 functions described in page 187…
nsamuel
  • 105
  • 1
  • 11
3
votes
2 answers

K&R What's the purpose of the buf array in reverse polish calculator

The buf array will never have more than one element, because of getop's implementation. So, couldn't it be declared as an ordinary char variable? This is the source code of the program, pages 74 - 79: #include int getch(void); void…
cristid9
  • 1,070
  • 1
  • 17
  • 37