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
8 answers

Confused understanding a passage about char and int types from K&R's "The C Programming Language"

Concerning this passage from Chapter 1: A Tutorial Introduction in Kernighan and Ritchie: The C Programming Language (I've bolded the specific part that I need clarification on and have elaborated down below): Given getchar and putchar, you can…
nf7
  • 31
  • 1
3
votes
3 answers

Not being able to get backspace character (\b) in the output on ubuntu (K&R example)

#include /* replacing tabs and backspaces with visible characters */ int main() { int c; while ( (c = getchar() ) != EOF) { if ( c == '\t') printf("\\t"); else if ( c == '\b') printf("\\b"); …
Ravi Yadav
  • 53
  • 1
  • 6
3
votes
2 answers

K&R C handling the octals

So, firstly I have read the following: http://www.hpc.unimelb.edu.au/nec/g1af02e/chap1.html It tells me the following: K&R C and SUPER-UX K&R C The digits 8 and 9 are allowed. They are considered decimal integer constants. I have got a little…
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
3
votes
4 answers

Writing in the location outside of array

I've just started learning programming. This is my first post. I'm reading a book "C Programming Language" by Kernighan and Ritchie, and I came across an example that I don't understand (section 1.9, p 30). This program takes text as input,…
MichaelSB
  • 3,131
  • 3
  • 26
  • 40
3
votes
2 answers

Help With K&Rs Counting Chars Example

I'm working my way through K&R's 2nd edition, and I've been stumped with this seemingly simple example: #include main(){ double c; for(c = 0; ((getchar() != EOF) && (getchar() != '\n')); ++c) ; …
Isaac
  • 15,783
  • 9
  • 53
  • 76
3
votes
1 answer

EOF exercise 1-6 K&R The C programming language

This is taken directly from the K&R book: The precedence of != is higher than that of =, which means that in the absence of parentheses the relational test != would be done before the assignment =. So the statement c = getchar() != EOF is…
Prafiate
  • 33
  • 4
3
votes
4 answers

In K&R 1.9 longest line example, what is getchar() doing?

I seem to understand the program now, except the getline function is not very intuitive as it seems to copy everything getchar() returns to a character array s[] which is never really used for anything important. int getline(char s[], int lim) { …
Leonardo
  • 1,452
  • 3
  • 15
  • 26
3
votes
3 answers

How come \b is not recognized from ex. 1-10 in K&R?

#include /* replace tabs and backspaces with visible characters */ main() { int c; while ((c = getchar()) != EOF) { if (c == '\t') printf("\\t"); if (c == '\b') printf("\\b"); if…
Joseph Lee
  • 529
  • 1
  • 5
  • 10
2
votes
1 answer

Why do the target-specific function declarations in GCC source use K&R style instead of prototypes?

I've been studying the GCC source code and I noticed that most (if not all) target-specific source code files (like arm.c for example) use the old K&R function style. Is there any particular reason for this? Backward compatibility? Portability?
2
votes
2 answers

K&R exercise 1-21

Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop,…
hansoko
  • 155
  • 9
2
votes
1 answer

K&R Quicksort issue

I seem to be having a problem understanding where the issue is in the qsort implementation by K&R (C Programming Language second edition). void qsort_1(int v[], int left, int right) { int i, last; void swap(int v[], int i, int j); if…
lukascobbler
  • 23
  • 1
  • 7
2
votes
1 answer

Can the function getline() from stdio.h coexist with the one in K&R88?

I'm knee-deep in [K&R88] and I get chided by gcc because the function getline(), which K&R use as example and practise material, is now in stdio.h (and has been since around 2010, I'm told.) Would there be any manner to tell the compiler to play it…
Éric Viala
  • 596
  • 2
  • 12
2
votes
1 answer

Having trouble understanding free() from K&R

I'm having a lot of trouble understanding what's going on inside the free function shown on chapter 8.7 from K&R, here's the full code and some information on how the program operates: The blocks are kept in order of increasing storage address, and…
qwerty_url
  • 535
  • 4
  • 12
2
votes
1 answer

Calculate range of variable types in C

a hobbyist here so sorry for the nooby question. K&R ex. 2-1. Use header constants and/or direct computation to calculate range of various variable types (char, short, int, long) + floating types. I used limits.h constants to print the range of…
user15480998
2
votes
1 answer

Kernighan and Ritchie C exercise 1-22 (fold long input lines)

I am trying to do the exercise about the line folding in the K&R C-book, but I can't go much forward. In particular, I do not knwo how to handle blank spaces inside the line i have to cut. The exercise is the following: "Write a program to ''fold''…
PwNzDust
  • 271
  • 2
  • 9