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

Why is variable being mutated in C program?

While working through exercise 3-5 in The C Programming Language, I've come across the following unexpected behavior. #include #include // inspired by:…
pdoherty926
  • 9,895
  • 4
  • 37
  • 68
2
votes
1 answer

Alignment issues when assigning the result of a sbrk to a pointer - K&R

referencing this code from Kernighan and Ritchie (2nd edition page 188), static Header* morecore(unsigned nu) { char *cp, *sbrk(int); Header* up; if (nu < NALLOC) nu = NALLOC; cp = sbrk(nu * sizeof(Header)); if (cp…
Curious
  • 20,870
  • 8
  • 61
  • 146
2
votes
6 answers

How do I complete K&R Exercise 2-4?

I'm learning how to write programs in C using the k&r book (The C Programming Language) and I have a problem with one of the exercises. It's asking me to detect and remove a character in string s1, which matches any characters in the string s2. So,…
ang
2
votes
0 answers

K&R C programming exercise 6.01 attempt giving wrong values

I am trying to do K&R programming exercise 6.01 in C ANSI version which states Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version. Below is the code I…
2
votes
2 answers

K&R Error: conflicting method definition

I am going through K&R (2 ed.) to learn C as I've been trying to get a basis in lower-level languages to help my programming and also because I want to know C. The book is absolutely fantastic; however, a program they provided on pp. 29 (Sec 1.9…
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
2
votes
2 answers

Why does a variable of a function declared outside its function definition doesn't throw an error?

Why is this Code with integer declaration in the middle of nowhere (in-between function definition), not throwing an error? 1) Why is it syntactically correct. 2) What is the use of doing so.? #include void func(int, int); int…
2
votes
3 answers

K&R atoi-general memory leak

I am following K&R second edition examples to learn C and coding as I think this is correct way of doing things. Anyhow when I run this program post compilation the program get stuck. I used valgrind for execution of compiled script. #include…
vector8188
  • 1,293
  • 4
  • 22
  • 48
2
votes
4 answers

Why won't this C program pick up escaped backslashes?

I'm doing K&R's Exercise 1-10 Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. I came up with this... #include…
alex
  • 479,566
  • 201
  • 878
  • 984
2
votes
6 answers

Implementing strcat() using pointers

I am reading K&R and trying to do the excercise involving writing a version of strcat (attach one string to the end of the other) using pointers. This is what I have: #include void mystrcat(char *s,char *t) { while(*s++); *s--; …
max2015
  • 113
  • 1
  • 11
2
votes
1 answer

Recursion Control flow

#include void printd(int n) { if(n/10) printd(n/10); putchar(n%10+'0'); } In the above code consider n as a positive integer and its value be 123. First time,123 is passed to printd (first printd) Second time,12 is passed…
ThunderPunch
  • 483
  • 1
  • 4
  • 16
2
votes
1 answer

Clarification in declaring an array

int getch(void); void ungetch(int); /* getop: get next character or numeric operand */ int getop(char s[]) { int i, c; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.') return c; /* not a…
ThunderPunch
  • 483
  • 1
  • 4
  • 16
2
votes
4 answers

Why do Kernighan and Ritchie include seemingly unnecessary typecasts?

Second edition. I'm looking at their hash table example in section 6.6. I found the full source transcribed here. This is the part I'm puzzling over: struct nlist *np; if((np=lookup(name))==NULL){ np=(struct nlist *)malloc(sizeof(*np)); Why…
Coquelicot
  • 8,775
  • 6
  • 33
  • 37
2
votes
2 answers

Why is some code from K&R not working in Code:Blocks?

Some of the examples in K&R don't work in Code:Blocks when I type them exactly. For example, this program: #include main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } When I type this…
2
votes
2 answers

Incompatible types in conditional expression when casting

I'm currently working my way through the K&R exercises, and there's something that's bugging me. I have the qsort function declaration: void qsort(void *v[], int left, int right, int (*comp)(void *, void *)); According to the book, I…
2
votes
4 answers

K&R 1-7 is it solvable by using putchar() instead of printf?

There are many questions about this exercise all over the internet, but I couldn't find any solution (nor any hint) on how to solve this exercise using 'putchar'. Write a program to print the value of EOF. I can easily get a working answer to…
maja
  • 697
  • 5
  • 18