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

Is it erroneous to reuse #define for the same identifier in c?

The K&R book author says that A second #define for the same identifier is erroneous unless the second token sequence is identical to the first. But when i tried , the compiler didn't raise any error. So, Can you please elaborate what does the…
Abhishek Jaiswal
  • 288
  • 2
  • 14
2
votes
2 answers

Why is string modified in C even when I'm not trying to modify it?

I'm trying to solve exercise 1-19 in K&R C second edition. "Write a function reverse that reverses the character string s. Use it to write program that reverses its input a line at a time." My solution takes two input strings s and t. s is source…
2
votes
3 answers

Checking for different characters in C

I wanted to know if there is a way in c to check, for example in a for function if a variable is equal or unequal to certain characters without having to repeat each time an equality check. If I am not wrong, checking equality this way is…
2
votes
3 answers

Can someone explains why this works to count set bits in an unsigned integer?

I saw this code called "Counting bits set, Brian Kernighan's way". I am puzzled as to how "bitwise and'ing" an integer with its decrement works to count set bits, can someone explain this? unsigned int v; // count the number of bits set in…
ggg123
  • 99
  • 10
2
votes
3 answers

Histogram of the length of words exercise hint?

I'm learning C with "The C Programming Language" book, and I'm trying to solve exercise 1.13: "Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical…
user731914
2
votes
1 answer

redeclaration of a functoin as int K&R 6.3

In chapter 6.3 of K&R the function getword() is defined like this: int getword(char *word, int lim) { int c, getch(void); void ungetch(int); char *w = words; while(isspace(c=getch())) ; if(c!= EOF) *w++;…
2
votes
2 answers

The program compiles, but when provided with an input, does nothing

I'm a beginner programmer (forgive this very basic question), and I am learning C through the Kernighan and Ritchie book "The C programming language". I copied this program from the book, and it compiles fine, but when an input is given, the program…
2
votes
2 answers

Reverse function is not working as expected

I am trying to figure out the program: "Write a function reverse(s) that reverses the characte string s. Use it to write a program that reverses its input a line at a time." I thought the pseudo code like following should work: "copy string s to…
readonly
  • 89
  • 7
2
votes
2 answers

Why don't Kernighan and Ritchie include int for the main functions?

Each example that is done is completed with main omitting "int". Why is this and why does it still compile the same without it. Are C compilers created with int implied?
Jinzu
  • 1,325
  • 2
  • 10
  • 22
2
votes
3 answers

Folding input lines every nth column (K&R 1-22) in C

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 something intelligent with very long lines, and if there are no…
2
votes
2 answers

Is this call by reference or by value in C?

I'm reading a book the c programming language authored by Brian W. Kernighan and Dennis M. Ritchie. The book lists code below void strcpy(char *s, char *t){ while((*s = *t) != '\0'){ s++; t++; } } and says: Because…
Andy Lin
  • 397
  • 1
  • 2
  • 21
2
votes
3 answers

Simple C Program

This program is based on the program in K&R in the input/output section #include main(){ double sum, v; sum = 0; while (scanf("%1f",&v)==1) printf("\t%.2f\n",sum+=v); return 0; } It compiles ok. But when trying to run, from any…
user485498
2
votes
3 answers

Why is it valid for the function to pass out a pointer in this K&R example?

I'm learning C by reading K&R (ANSI edition), supplemented with 21st Century C. I'd say I'm already pretty confident with most of the fundamentals of pointers. That means I know you have to be very careful passing pointers out of a function that…
Igid
  • 515
  • 4
  • 15
2
votes
2 answers

function for reversing an array in C (K&R 2nd ed.)

Trying to do Exercise 1-19 of K&R 2nd ed., e.g. writing a function to reverse a string. I thought I managed, but the print output looks strange :-) If I use STRINGSIZE 5 the output is Original String: hello Reversed String: ollehhello. If I use…
Mario Christov
  • 141
  • 1
  • 8
2
votes
2 answers

Extracting unique elements in an array (from K and R C ex1-14)

Returning C newb here again. I am trying my hand at the exercises in K and R C and on my way to trying exercise 1-14, I am really stumped. My solution works but is not always correct, I am seeking help to refine what I have written, or if there is a…
Unpossible
  • 603
  • 6
  • 23