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

Putchar and Getchar in C

I'm reading K&R's The C Programming Language and have become confused on putchar and getchar. I made a program where you enter 10 chars and the program prints them back out to the screen. #include int main() { int i; int ch; …
CS Student
  • 1,613
  • 6
  • 24
  • 40
7
votes
7 answers

Where does `getchar()` store the user input?

I've started reading "The C Programming Language" (K&R) and I have a doubt about the getchar() function. For example this code: #include main() { int c; c = getchar(); putchar(c); printf("\n"); } Typing toomanychars + CTRL+D…
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
6
votes
8 answers

Hex to Decimal conversion [K&R exercise]

I'm learning C and I can't figure out one of the K&R exercises, the listing: Exercise 2-3, Write the function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The…
CBegin
6
votes
3 answers

Pointer type mismatch warning in example from K&R C

Possible Duplicate: Problem compiling K&R example Lately I have been working my way through the C Programming Language by K&R. In section 5.11 they cover pointers to functions and after typing in their example -- a quicksort implementation where…
Dana
  • 32,083
  • 17
  • 62
  • 73
6
votes
2 answers

Binary tree implementation in C question as found in K&R

So I've been reading through the K&R C book and have a question.. in the 6th Chapter on structs on page 140-141, there is code that looks like this (I took out some of the more irrelevant parts) /* the program loops through a tree looking for some…
adelbertc
  • 7,270
  • 11
  • 47
  • 70
6
votes
4 answers

getop() function K&R book p 78

I'm studying K&R book. Currently i'm reading function getop() at p.78. I do understand the code but i need clarifications about 2 things. The code of getop() is as follows: int getch(void); void ungetch(int); /* getop: get next character or…
Hussein Barada
  • 117
  • 1
  • 11
6
votes
1 answer

Computing floating point accuracy (K&R 2-1)

I found Stevens Computing Services – K & R Exercise 2-1 a very thorough answer to K&R 2-1. This slice of the full code computes the maximum value of a float type in the C programming language. Unluckily my theoretical comprehension of float values…
maja
  • 697
  • 5
  • 18
6
votes
2 answers

Type conversion: signed int to unsigned long in C

I'm currently up to chapter 2 in The C Programming Language (K&R) and reading about bitwise operations. This is the example that sparked my curiosity: x = x & ~077 Assuming a 16-bit word length and 32-bit long type, what I think would happen is 077…
SpruceMoose
  • 99
  • 1
  • 5
6
votes
3 answers

K&R 2nd Edition, Example 1.9 Character Arrays

I have a question in regards to the getline() function and parameter definition in the following code. The code is taken directly from K&R chapter 1.9: "Character arrays". I have reproduced it here verbatim. The issue is that when I compile the…
Deesbek
  • 865
  • 2
  • 12
  • 27
6
votes
2 answers

Using `read` system call on a directory

I was looking at an example in K&R 2 (8.6 Example - Listing Directories). It is a stripped down version of Linux command ls or Windows' dir. The example shows an implementation of functions like opendir, readdir. I've tried and typed the code…
5
votes
5 answers

Please Explain this Example C Code

This code comes from K&R. I have read it several times, but it still seems to escape my grasp. #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch(void) { return(bufp>0)?buf[--bufp]:getchar(); } int ungetch(int c) { …
user485498
5
votes
5 answers

K&R: array of character pointers

On pg. 109 of K&R, we see: void writelines(char *lineptr[], int nlines) { while (nlines -- > 0) printf("%s\n", *lineptr++); } I'm confused about what *lineptr++ does exactly. From my understanding, printf requires a char pointer, so we provide…
Elben Shira
  • 2,086
  • 3
  • 14
  • 13
5
votes
4 answers

What is the use of a pointer to function as shown in K&R example

In the K&R ANSI C book I have stumbled upon a piece of code where a pointer to a function is used. I think I understand the idea behind pointers to functions, but the example presented in the book does not make sense to me. The function is to sort…
Damian Kowalski
  • 362
  • 2
  • 8
5
votes
3 answers

Difference between struct S { int align; }; (name after struct keyword) and struct { int align; } S; (name after the struct definition)

#include struct Header { unsigned long long int alignment; }; int main(void) { struct Header header; // note: we can loose the 'struct' in C++ struct Header* pheader = &header; return 0; } The program above compiles…
KeyC0de
  • 4,728
  • 8
  • 44
  • 68
5
votes
2 answers

Confusing line in K&R 5.11 function pointers C

The last parameter of this line in main() makes me lost // declaration void qsort(char *linep[], int left, int right, int (*compare)(void *, void*); // use main(){ qsort((void**) lineptr, 0, nlines-1, (int (*)(void*,void*))(numeric ? numcmp…
1 2
3
22 23