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
4
votes
1 answer

KR - visualize backspace

I come across this KR exercise 1-10: Write a program to copy its input to its output, replace each tab by \t, each backspace by \b, and each backslash by \\ . Here's a very simple solution: #include int main() { int c; const…
artm
  • 17,291
  • 6
  • 38
  • 54
4
votes
3 answers

Clarification in getop()

In Dennis Ritchie's "C programming Language" book, In getop func, he states that s[1]='\0' why does he end the array on index 1? What's the significance and need? In later part he does uses other parts of the array.. int getch(void); void…
ThunderPunch
  • 483
  • 1
  • 4
  • 16
4
votes
2 answers

I need help understanding what Exercise 5-12 is asking for in the C Programming Language book

K&R C Programming Language: pg. 105 Extend entab and detab to accept the shorthand entab -m +n to mean tab stops every n columns, starting at column m. entab replaces a number of spaces with a tab character and detab does the opposite. The…
marsol0x
  • 715
  • 1
  • 6
  • 5
4
votes
2 answers

Is there an error in the example code in chapter 1.9 in the classic book "The C Programming Language"?

In the Chapter 1.9 in the classic book about C language "The C Programming Language" by Brian & Dennis, there is a bunk of code about a function 'getline' which is used to copy the next line of input text into a char type string and check the…
microbit
  • 319
  • 3
  • 10
4
votes
3 answers

Assigning values to pointers?

In Dennis Ritchie I found this, struct rect r , *rp = r; then these four expressions are equivalent : r.pt1.x rp->pt1.x (r.pt1).x (rp->pt1).x because operators associate left to right. Shouldn't it be…
akash
  • 1,801
  • 7
  • 24
  • 42
4
votes
5 answers

Bit invert function in K&R exercise 2-7

Exercise 2-7 of The C Programming Language: Write a function invert(x,p,n) that returns x with the n bits that begin at position p inverted (i.e., 1 changed to 0 and vice versa), leaving the others unchanged. I understood the question like this:…
sdf89
  • 65
  • 1
  • 8
4
votes
3 answers

K and R exercise 1-24

I am doing programs in The C Programming Language by Kernighan and Ritchie. I am currently at exercise 1-24 that says: Write a program to check a C Program for rudimentary syntax errors like unbalanced parentheses, brackets and braces. Don't…
Sam
  • 1,842
  • 3
  • 19
  • 33
4
votes
3 answers

recursive printf of binary tree elements

I've come back to K&R in order to read one chapter, and noticed an example I had previously omitted. The chapter covers the topic of a binary tree data type. I understand storing new entries in the nodes, but printing function gives confuses me. Why…
Peter Cerba
  • 806
  • 4
  • 14
  • 26
4
votes
3 answers

Beginner Doing K&R

I'm just starting programming and going through K&R to try and learn C. I've gotten to the section on command line arguments (5.10) but now I'm stumped. Every time I try and open a program I've written with command line arguments I'm told that…
Michael
  • 462
  • 1
  • 6
  • 18
4
votes
2 answers

K & R malloc code doesn't make sense?

This code is from the K & R book - Chapter 8 Section 7: Example - Storage Allocator. This code, at least for me, doesn't make sense. "Header" is a union of a struct and a "most restrictive alignment type", which is a long type. Malloc will then find…
1der
  • 185
  • 1
  • 3
  • 12
3
votes
4 answers

K&R Exercise 2-3 "Hex to int converter" Problem

The program I wrote works in demographics consisting of only single Hexadecimal values. (Probably not the most elegant solution, but I'm a new programmer) My question is, how would I go about handling of multiple hexadecimal digits, such as 0xAF,…
F0D
3
votes
1 answer

What's wrong with my solution to K&R exercise 1-22?

Exercise 1-22 of The C Programming Language is as follow: Write a program to "fold" long input lines into two or more shorter lines after the last non-blank character that occurs before the n-th column of input. Make sure your program does…
Javier
  • 4,552
  • 7
  • 36
  • 46
3
votes
3 answers

Difficulties with an example 1.9 of The C Programming Language

I'm am working my way through the exercises of the first chapter of The C Programming Language and while I understand most of what is said and shown, there is one example that I don't understand. In 1.9, there is a function shown to return the…
3
votes
1 answer

How were arrays initialized in K&R C?

In K&R C(2nd) 127p, The main change made by the ANSI standard is to define structure assignment-structures may be copied and assigned to, passed to functions, and returned by functions. This has been supported by most compilers for many years, but…
op ol
  • 705
  • 4
  • 11
3
votes
1 answer

Problem with K&R C book regarding how scanf deals with blanks and tabs in the format string?

Reading the famous book The C programming language ANSI C second edition by Brian Kernighan and Dennis Ritchie, I found in chapter 7 (section 7.4. page 157) this paragraph below which describe the format string for scanf: [...] The format string…