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

The C Programming Language (K&R) ex1-20 . I encounter some trouble

/* * 1-20. 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. * Should n be a variable or a symbolic parameter? …
wy-ei
  • 21
  • 1
2
votes
1 answer

For casting an array of pointers (e.g *array[]), why use (void**) instead of (void*)

The following code snippet is from K&R Chapter 5-11: Pointers to Functions: qsort((void**) lineptr, 0, nlines-1, (int (*)(void *, void *)(numeric ? numcmp : strcmp)); I am able to compile/run the code with (void*) so why is lineptr cast with…
Kevin
  • 97
  • 9
2
votes
2 answers

Unexpected Output for K&R Exercise 1-18 (The C Programming Language)

I am rather new to programming so please bear with me. Here is the prompt for exercise 1-18: Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. And here is my code: #include…
jhschwartz
  • 168
  • 1
  • 2
  • 13
2
votes
2 answers

datatype of variable used for holding input of getchar(),as char instead of int

#include int main() { int c; while((c=getchar())!=EOF) putchar(c); putchar(c); //2nd putchar getch(); } In the above code from the book 'C by Ritchie n Kernighan', the reason for declaring c an int…
kasif
  • 57
  • 1
  • 8
2
votes
7 answers

Custom get line input function

I am reading over the K&R book, and am a little stuck. What is wrong with the following? void getInput(int* output) { int c, i; for(i=0; (c = getchar()) != '\n'; i++) output[i] = c; // printf("%c", c) prints the c value as expected …
chris
  • 4,332
  • 5
  • 41
  • 61
2
votes
1 answer

What is the purpose in declaring a function with different named arguments than the function itself uses?

Perhaps this is just for showing. While reading K&R, the beginning of 1.7 Functions shows an example of a power function. The second line of code declares int power(int m, int n); Why is the first argument, n, named differently than the first…
Leonardo
  • 1,452
  • 3
  • 15
  • 26
2
votes
3 answers

C programming - K&R example 1.5.2 - modified program not functioning as intended

My question is simply "Why does my code on line 10 and 11 not function properly?" The intended purpose of my code is to do exactly as the original K&R code intended, but to NOT count nc whenever (getchar() == '\n') will you please enlighten…
pying saucepan
  • 183
  • 1
  • 1
  • 9
2
votes
1 answer

no output from read() combined into getchar

I've asked a question regarding the theoretical aspect of this code a couple of hours ago. Now I understand everything, however, that code simply gives no output. Here's the link to the code on http://ideone.com/LWMC5 Code: #include…
Peter Cerba
  • 806
  • 4
  • 14
  • 26
1
vote
1 answer

Section 5.10 from Kernighan/Ritchie Command line arguments/optional parameters

first time poster. Was hoping someone could help me with this. In section 5.10, Kernighan gives this example of a program that reprints lines of text with the string in them. So I saved this as "find" in my folder, and then went into cmd, then the…
1
vote
4 answers

how does this code from "The C Programming Language" work?

I'm reading "The C Programming Language (2nd ed.) and near the beginning, it has examples like this: while((c = getchar()) != EOF) if(c == '\n'){ ++n1; I can see how this would work while reading from a file, and I understand this…
Carson Myers
  • 37,678
  • 39
  • 126
  • 176
1
vote
3 answers

Returning a pointer to a struct

I came across a example returning a struct in 'C Programming Language' by Kernighan & Ritchie. /* binsearch: find word in tab[0]...tab[n-1] */ struct key *binsearch(char *word, struct key *tab, int n) { int cond; struct key *low = &tab[0]; …
Akash
  • 4,956
  • 11
  • 42
  • 70
1
vote
2 answers

Very basic example code of "The C Programming Language" doesn't work like expected?

I'm a middle experienced Java developer and have many problems learning the C language for my computer science study. I try it with the book "The C Programming Language" which many people seem to recommend. But I've got problems with the simplest…
CGee
  • 1,650
  • 5
  • 20
  • 31
1
vote
2 answers

Why do I need to explicitly putchar (' ') in this code?

I am working through Kernighan & Ritchie and have got to exercise 1.9. In fact I wrote some code which appears to solve the exercise, and I have tested it on Windows (with Git Bash and gcc) and Termux (with clang) by piping in a line with a variable…
harlandski
  • 396
  • 3
  • 6
1
vote
2 answers

Is it really legal for K&R to write "PFI strcmp, numcmp;" where PFI is typedef'd as "int (*)(char *, char *)"?

In The C Programming Language (Kernighan and Ritchie, 2nd ed) on p147, the authors show a typedef declaration typedef int (*PFI)(char *, char *); (PFI stands for "pointer to function returning an int"), which they claim can be used in contexts…
Lover of Structure
  • 1,561
  • 3
  • 11
  • 27
1
vote
3 answers

Stumped by K&R exercise 1.5.2

I am currently trying to learn C by using the K&R, but I am completely stumped by example 1.5.2. For some reason, after I press Ctrl-Z, instead of printing nc, it prints nc multiplied by 2. I don't know what could be causing this problem (I copied…
Emryss
  • 13
  • 4